question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FastAPI always returns content, even if 204 no content status code is set.

See original GitHub issue

First 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:closed
  • Created 3 years ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

12reactions
falkbencommented, Feb 18, 2021

You could instead set the response class for that path operator to be Response instead of the default JSONResponse. Then it won’t convert None into JSON. So w/ your example above it would be:

@app.get("/", status_code=HTTP_204_NO_CONTENT, response_class=Response)
def read_root():
    print("d")

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

6reactions
aviramhacommented, Feb 18, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found