【问题标题】:How to read a JSON payload from a DELETE request on Sanic Framework?如何从 Sanic Framework 上的 DELETE 请求中读取 JSON 有效负载?
【发布时间】:2021-12-17 11:07:23
【问题描述】:

在前端,我正在发送一个带有 JSON 有效负载的 DELETE 请求。这工作正常并且数据被正确发送,但在后端 - 使用 Sanic 框架 - 请求的正文是空的。

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

如何从 DELETE 请求中访问请求的正文?

【问题讨论】:

    标签: sanic


    【解决方案1】:

    默认情况下,Sanic 不会使用 DELETE 的主体。有两种选择:

    选项 #1 - 告诉 Sanic 消耗身体

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

    选项 #2 - 在处理程序中手动使用主体

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

    Release notes API docs

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多