New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Response content longer than Content-Length error for DELETE and NoContent #4939
Comments
This should help: #717 (comment) |
This works as well: from fastapi import FastAPI, status, Response
app = FastAPI()
@app.delete("/", status_code=status.HTTP_204_NO_CONTENT, response_class=Response)
def read_root():
return None |
Thanks @aboubacs! I was having the same issue returning 204 as a POST response. @tiangolo This worked as recently as 0.75.2 without a special response class. IIRC, we had this same problem a long time ago before I contributed the special code for recognizing 204 as a no-content response. My opinion is that 204 as the declared status code should still work without the |
OK, so I looked into this a bit and the issue looks to be related to a recent change in Starlette (see this discussion for a repro without FastAPI in the mix). It may be that Starlette used to suppress the content return on a JSONResponse if the response code was 204, but it no longer does. I notice that, historically, there are a number of times when people have proposed adding code to FastAPI's router to set the response class explicitly (to |
It's worth noting that it's the 3rd issue related to this topic created this week, so the current state of things is a loss of time for many users |
Does FastAPI not have any regression tests? How is it that this wasn’t picked up when the starlette version was bumped? |
This appears to be fixed somewhat in 0.79.0, as now if the status code is one that doesn't allow content, it will explicitly set the response body to |
hi I get the same error when doing: raise HTTPException(status_code=HTTP_304_NOT_MODIFIED) I am running fastapi 0.79.0 |
I think #4952 is a fix for this |
This seems to be an issue for me still on fastapi 0.82.0. It can be reproduced like this: from fastapi import FastAPI, HTTPException
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
raise HTTPException(204)
if __name__ == '__main__':
uvicorn.run(app, debug=True) Visiting the page results in:
|
Any updates on this? |
I cannot reproduce with the example of @gazpachoking . This is my cURL: [jarro@MBP-van-Jarro ~ % curl localhost:8000 -v
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 204 No Content
< date: Sun, 18 Sep 2022 20:50:13 GMT
< server: uvicorn
<
* Connection #0 to host localhost left intact Which seems correct. My logs are clean as well:
Running on Python 3.10.5, uvicorn 0.18.2, FastAPI 0.83:
|
FWIW, I don't believe the behavior reported by @gazpachoking, even if it's reproducible, is a bug. 204 is an "OK" status response with no body. All request error (400-series) and server-error (500-series) responses are expected to have a body which explains the problem, and the |
This was discussed and handled in #4949 and #5365.
Hmm, that's a good point. It isn't really an exception, although I still think it's nice to be able to short circuit the view function the same way you can for an actual exception. |
I completely agree, and I agree that |
I ran into this issue when using a custom response class that was returning a string with unicode characters instead of returning bytes. For example to reproduce:
|
First Check
Commit to Help
Example Code
Description
Upon requesting above code I got expected response but my logs shows that there is an error in uvicorn. The problem exists for DELETE method and NoContent response status code (for HEAD there is no such problem)
The text was updated successfully, but these errors were encountered: