Skip to content

HTTP 204s do not conform to HTTP spec #3013

@mikegrima

Description

@mikegrima

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.

Description

Hello! I have noticed that fastAPI's HTTP 204's No Content response does not appear to conform to the HTTP spec by default. Per the HTTP spec: https://tools.ietf.org/html/rfc2616#section-10.2.5:

   The 204 response MUST NOT include a message-body, and thus is always
   terminated by the first empty line after the header fields.

It appears that fastapi is attempting to serialize as a JSON, and thus adding data to the message body. Here is an example that you can use to reproduce:

from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.get("/")
def read_root():
    raise HTTPException(status_code=204)

Curling this will result in:

curl -v localhost:8000/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.64.1
> Accept: */*
> 
< HTTP/1.1 204 No Content
< date: Mon, 29 Mar 2021 20:28:47 GMT
< server: uvicorn
< content-length: 23
< content-type: application/json
< 
* Excess found in a non pipelined read: excess = 23 url = / (zero-length body)
* Connection #0 to host 127.0.0.1 left intact
* Closing connection 0

This line indicates the issue:

* Excess found in a non pipelined read: excess = 23 url = / (zero-length body)

It looks as though it's adding the following to the body:

{"detail":"No Content"}

Expected Behavior

The expected behavior is to not return anything in the body. This behavior should be the default when handling HTTPException of a status code of 204.

The current behavior can cause issues if you have your service being fronted with a proxy that strongly enforces the HTTP spec.

I don't have time today to attempt to fix, but would be happy to.

Environment

  • OS: [e.g. Linux / Windows / macOS]: macOS
  • FastAPI Version [e.g. 0.3.0]: 0.63.0
  • Python version: 3.8.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions