参考
https://stackoverflow.com/questions/69669808/fastapi-custom-middleware-getting-body-of-request-inside
原因:框架问题
处理思路:手动赋予request的一个属性,然后再获取
async def set_body(request: Request, body: bytes):
async def receive():
return {"type": "http.request", "body": body}
request._receive = receive
async def get_body(request: Request) -> bytes:
body = await request.body()
await set_body(request, body)
return body
try:
await set_body(request, await request.body())
body = await get_body(request)
logger.info(f" Body: {body.decode()}")
except Exception as e:
logger.info(f" Body : {e}")