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 the 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
According to MDN documentation
Similarly, as described in RFC 9110: HTTP Semantics
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
Locationheader 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.