简体   繁体   English

如何从 Sanic Framework 上的 DELETE 请求中读取 JSON 有效负载?

[英]How to read a JSON payload from a DELETE request on Sanic Framework?

On the frontend, I'm sending a DELETE request with a JSON Payload.在前端,我正在发送一个带有 JSON 有效负载的 DELETE 请求。 This works fine and the data is correctly send, but in the backend - using Sanic Framework - the request's body is empty.这工作正常并且数据被正确发送,但在后端 - 使用 Sanic 框架 - 请求的主体是空的。

print(request.body) # b''
print(request.json) # None

How can I access the request's body from a DELETE request?如何从 DELETE 请求访问请求的正文?

Thanks in advance!提前致谢!

By default, Sanic will not consume the body of a DELETE.默认情况下,Sanic 不会使用 DELETE 的主体。 There are two alternatives:有两种选择:

Option #1 - Tell Sanic to consume the body选项 #1 - 告诉 Sanic 消耗身体

@app.delete("/path", ignore_body=False)
async def handler(_):
    ...

Option #2 - Manually consume the body in the handler选项 #2 - 在处理程序中手动使用主体

@app.delete("/path")
async def handler(request: Request):
    await request.receive_body()

Release notes API docs 发行说明API 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM