English 中文(简体)
利用快车道,我怎么能领到药房公司的原始要求?
原标题:Using FastAPI in a sync way, how can I get the raw body of a POST request?

sync上使用快车牌,而不是<条码>。 顺便说一句,我谨收到一份无改动的无资格证书申请。

All examples I can find show async code, when I try it in a normal sync way, the request.body() shows up as a coroutine object.

当我通过将一些<代码>XML上贴到这个终点时,我收到一份<代码>500“Internal服务器 Error”

from fastapi import FastAPI, Response, Request, Body

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.post("/input")
def input_request(request: Request):
    # how can I access the RAW request body here?  
    body = request.body()

    # do stuff with the body here  

    return Response(content=body, media_type="application/xml")

Is this not possible with FastAPI?

注:简化投入请求将考虑:

POST http://127.0.0.1:1083/input
Content-Type: application/xml

<XML>
    <BODY>TEST</BODY>
</XML>

我没有控制如何发出投入要求,因为我需要取代现有的《社会行动计划》的预报。

最佳回答

Using async def endpoint

<205> 例如:

from fastapi import Request

@app.post("/input")
async def input_request(request: Request):
    return await request.body()

Update 1 - Using def endpoint

或者,如果你相信新数据是有效的<代码>JSON,则你可以将结尾点与<代码>def加以界定,并使用>>>:>Body数据,见

from fastapi import Body

@app.post("/input")
def input_request(payload: dict = Body(...)):
    return payload

但是,如果输入的数据载于XML/code>格式,如你提供的例子,一种选择是使用> 编码,只要你控制客户数据如何发送给服务器(请查询

from fastapi import File

@app.post("/input") 
def input_request(contents: bytes = File(...)): 
    return contents

Update 2 - Using def endpoint and async dependency

,。 从请求中删除<代码>。 您也可使用<代码>sync Dependencies on non-async (i.e.,def) endpoints。 因此,如果在此结尾处存在某种阻塞代码,则阻止你使用>。 我猜想这或许是你的理由——这是前进的道路。

注:我还应提及:,该回答是——其中解释了defsync def终点(你可能知道)之间的区别——也提供了在您需要使用<条码>Aasync def时的解决办法。 (由于您可能需要在一条线路上安装有关校正的<代码>await<>/code>),但也有一些synchronous 昂贵的CPU限制操作,可能会阻挡服务器。 请看一看。

上文描述的办法实例如下。 如果你要确认,请求获得的笔数将阻碍通过sync def

from fastapi import FastAPI, Depends, Request
import time

app = FastAPI()

async def get_body(request: Request):
    return await request.body()

@app.post("/input")
def input_request(body: bytes = Depends(get_body)):
    print("New request arrived.")
    #time.sleep(5)
    return body
问题回答

为方便起见,您仅可使用asgiref ,本套支持async_to_syncsync_to_async:

from asgiref.sync import async_to_sync

sync_body_func = async_to_sync(request.body)
print(sync_body_func())

async_to_sync 在一次活动时执行同文职能,sync_to_async 在校友中履行yn功能。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签