FastAPI always returns content, even if 204 no content status code is set.
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
Example
from fastapi import FastAPI
from starlette.status import HTTP_204_NO_CONTENT
app = FastAPI()
@app.get("/", status_code=HTTP_204_NO_CONTENT)
def read_root():
print("d")
Description
Send a request with any HTTP Client, observe the traffic using tcpdump/wireshark - you’ll see that you get null as a response (content-length will be 4). This is extremely notable as this caused this issue https://github.com/encode/httpx/issues/1474 (The client doesn’t expect any data in that case) I would expect FastAPI to not return null when response is None.
Environment
- OS: macOS
- FastAPI Version [e.g. 0.3.0]: 0.63.0
- Python version: 0.63.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:23 (11 by maintainers)
Top Results From Across the Web
Response Status Code - FastAPI
A special case is 204 , "No Content". This response is used when there is no content to return to the client, and...
Read more >tiangolo/fastapi - Gitter
Well whatever it is, 204 should not return b'null' I'd be tempted to say. ... question is what FastAPI does when you return...
Read more >API response of FastAPI delete call - python - Stack Overflow
You can omit the return entirely, as the return code 204 is bound to not return any content. FastAPI replaces any content in...
Read more >Create A Delete Request with FastAPI - Better Programming
Why do we return a 204 and not a 200 response? Well, in this case, we don't return a body. Therefore, a 204...
Read more >Introducing FARM Stack - FastAPI, React, and MongoDB
The final router does not return a response body on success, as the requested document no longer exists as we have just deleted...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You could instead set the response class for that path operator to be
Response
instead of the defaultJSONResponse
. Then it won’t convertNone
into JSON. So w/ your example above it would be:which correctly returns
b""
.note, if you are doing this on multiple endpoints, you can set the default response class on your router, or even your entire app. https://fastapi.tiangolo.com/advanced/custom-response/#default-response-class
Thanks @falkben. I found this solution also but I believe it should be handled by the framework. It’s fairly simple to implement hence the PR I submitted. Returning 204 with data is invalid according to the standards so FastAPI should detect such scenario and handle it accordingly IMO