[Python] What should HTTP 201 response body be when responding to a POST request with large...

Discussão em 'Python' iniciado por Stack, Fevereiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have a FastAPI server implemented in Python, and I am sending large data (~2 GB) to the server via a POST request. Once the request is processed on server side, I would like to return a 201 "Created" response to the client.

    A 201 response should have a body, so I should not send a response with an empty body (https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST), but at the same time, I don't want to send back the newly created resource, as it is so large, and the client doesn't need the resource at the point of the POST request. Alternatively, I would be tempted to respond with a 204 "No Content", but it doesn't seem to be the recommended response for a successful POST request.

    So assuming a 201 response is sent, what should be the body of the response to avoid large data sent back for no reason?

    Here is a minimal code describing my issue, currently returning a 201 response with an empty body:

    from fastapi import FastAPI, HTTPException, Response, status

    app = FastAPI()


    @app.post("/data", status_code=status.HTTP_201_CREATED)
    async def send_data(data, response: Response):
    try:
    # process data on the server
    # ...
    pass
    except Exception as e:
    raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, e)
    return response # response with empty body

    Continue reading...

Compartilhe esta Página