Please add additional documentation on returning status code 204 ("Too much data for declared Content-Length" error) #10655
Replies: 1 comment
-
|
Solution: #8010 (comment) Thanks for the idea! We will consider adding this clarification in docs |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
# Sample code provided in links in descriptionDescription
Hello,
In FastAPI, if you develop a function which should return status code 204, you may end up encountering this error:
The problem, as I understand it, is this:
Similar issues have been found here:
There is an answer given on the first discussion to use a decorator (ex.
@app.get("/", status_code=204)), but in my case I was usingapp.add_api_route()and thus not sure how to add a status code. And as another commenter says, they need to return different status codes depending on some logic, and it wasn't apparent how to do that and avoid error. (when I did try to switch to using a decorator, the app returned a 422 status and error message with some Pydantic json blob, rather than the 204 status I was hoping for, but maybe that's some other problem?)The "correct" solution appears to be to change one's code to return a
Response(status_code=HTTPStatus.NO_CONTENT.value), rather than aJSONResponse("", status_code=204)orHTTPResponse. This avoids whatever extra processing is adding extra bytes in the response.My request is that the documentation (here and here) be updated to include instructions to use the
Responsefor HTTP 204 status codes, with either a brief explanation of the issue or a link to this discussion. Based on the number of other issues, and my own time trying to track down a solution for this, I think this additional documentation would save people a fair bit of time.It would be nice if the JSONResponse/HTTPResponse/etc knew not to return a body for status code 204 (or whatever additional processing it's doing; just returning
Noneor""as the body seems to still cause the error), but I'm not trying to make more work for anybody.Thanks very much for your consideration 🙏
(p.s. servers which don't use h11, like hypercorn(?), don't hit this error, which is interesting; some people might be running into this bug and just not knowing it because their server's http library isn't as robust)
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.104.1
Pydantic Version
2.5.1
Python Version
Python 3.11.6
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions