sanic

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. This works fine and the data is correctly send, but in the backend - using Sanic Framework - the request's body is empty.

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

How can I access the request's body from a DELETE request?


Solution

  • By default, Sanic will not consume the body of a DELETE. There are two alternatives:

    Option #1 - Tell Sanic to consume the body

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

    Option #2 - Manually consume the body in the handler

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

    Release notes API docs