pythonhttppostfastapihttpresponse

What should HTTP 201 response body be when responding to a POST request with large data?


I have a server implemented using FastAPI 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

Solution

  • According to MDN documentation

    201 Created

    The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource. The new resource, or a description and link to the new resource, is effectively created before the response is sent back and the newly created items are returned in the body of the message, located at either the URL of the request, or at the URL in the value of the Location header.

    The common use case of this status code is as the result of a POST request.

    Similarly, as described in RFC 9110: HTTP Semantics

    The 201 (Created) status code indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either a Location header field in the response or, if no Location header field is received, by the target URI.

    The 201 response content typically describes and links to the resource(s) created. Any validator fields (Section 8.8) sent in the response convey the current validators for a new representation created by the request. Note that the PUT method (Section 9.3.4) has additional requirements that might preclude sending such validators.

    Hence, in short, one does not really have to return the actual data of the resource created back to the client, but rather a description and/or a link to the new resource in the Location header would be sufficient. The description in the response, among others, could include the length of the data received by the server, thus informing the user that the upload was indeed successful.