Issue 1:
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

Issue 2:
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

Here's a self-contained, minimal, reproducible, example with my use case:

from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.get("/")
def read_root(gimme_coffee: bool = False):
if gimme_coffee:
raise HTTPException(status_code=418, detail="I'm a teapot.")
return {"Hello": "World"}

Description

Open the browser and call the endpoint /docs

Note that the only listed response codes are 200 and 422

Execute the route with gimme_coffee set to true and note that it returns a 418 status code

How do I set the status code for 418 in the OpenAPI docs? I was expecting to be able to pass in a list of possible HTTPExceptions and have them be automatically converted, but there does not appear to be any way to do that. The closest thing I found was Additional Status Codes but that seemed at best tangentially relevant since I am raising HTTPExceptions, not looking to build out a custom exception JSON response.

Properly documenting error states is just as important as documenting the success state. I really hope FastAPI provides an easy way to do this.

Environment

OS: Linux (Docker container using standard python-3.8 image)

FastAPI Version: 0.61.1

Python version: 3.8.5