在FastAPI中,无需使用async await即可访问request.body()参数。

huangapple go评论72阅读模式
英文:

Access request.body() parameters without async await in FastAPI

问题

我正在尝试在后端路由上不使用异步方式接收来自ajax调用的请求体参数。

在使用异步方式时,它可以正常工作:

@app.post('/process-form')
async def process_form(request: Request):
    # 访问表单数据
    form_data = await request.body()
    da = jsonable_encoder(form_data)
    print(da)

但是在不使用异步方式时,body() 方法不存在(VS Code调试器中),而且似乎是可等待的:

@app.post('/process-form')
def process_form(request: Request):
    # 访问表单数据
    form_data = request.body()
    da = jsonable_encoder(form_data)
    print(da)
site-packages\anyio\streams\memory.py", line 89, in receive_nowait
    raise WouldBlock
anyio.WouldBlock

为什么会这样,并且如何从请求中接收我的数据呢?

英文:

I am trying to receive body parameters from ajax call without using async on backend route.

When doing async It is working fine:

@app.post('/process-form')
async def process_form(request : Request):
    # Access form data
    form_data = await request.body()
    da = jsonable_encoder(form_data)
    print(da)

But without async body() does not exist (vs code debugger) and it is awaitable or something:

@app.post('/process-form')
def process_form(request : Request):
    # Access form data
    form_data = request.body()
    da = jsonable_encoder(form_data)
    print(da)
site-packages\anyio\streams\memory.py", line 89, in receive_nowait
    raise WouldBlock
anyio.WouldBlock

Why is it so and how Could I receive my data from request?

答案1

得分: 1

以下是代码的翻译部分:

# foo.py

from asyncio import run
from fastapi import FastAPI, Request

app = FastAPI()

@app.post('/')
def process_form(request: Request):
    body = run(request.body())
    print(body)

我使用以下命令运行应用程序:

$ uvicorn --host 0.0.0.0 --port 9999 --reload foo:app

并使用以下命令进行测试:

$ curl http://localhost:9999/ -XPOST --data-raw 'hello world'

关键是使用 asyncio.runpython3.9 的文档链接)来“同步执行”协程和任务。

英文:

Does the following code accomplish what you are looking for?

# foo.py

from asyncio import run
from fastapi import FastAPI, Request

app = FastAPI()

@app.post('/')
def process_form(request: Request):
    body = run(request.body())
    print(body)

I run the app with:

$ uvicorn --host 0.0.0.0 --port 9999 --reload foo:app

And test with:

$ curl http://localhost:9999/ -XPOST --data-raw 'hello world'

<hr />

The key is to use asyncio.run (documentation for python3.9) to "execute synchronously" coroutines and tasks.

答案2

得分: 0

你可以这样做以获得同步端点并访问有效负载:

@app.post('/process-form')
def process_form(form_data: Any, request : Request):
    # 访问表单数据
    print(form_data)

form_data 将包含你发送的数据作为请求的有效负载。
你需要正确地对 form_data 进行类型标注。

我认为这个帖子与你的问题类似: https://stackoverflow.com/questions/70658748/using-fastapi-in-a-sync-way-how-can-i-get-the-raw-body-of-a-post-request

英文:

You can do this to have a sync end-point and access the payload:

@app.post(&#39;/process-form&#39;)
def process_form(form_data: Any, request : Request):
    # Access form data
    print(form_data)

form_data will contain the data you send as a payload to your request.
You'll need to correctly type form_data

I think this post is similar to your question https://stackoverflow.com/questions/70658748/using-fastapi-in-a-sync-way-how-can-i-get-the-raw-body-of-a-post-request

huangapple
  • 本文由 发表于 2023年6月15日 14:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定