Skip to content

Include 'Allow' header on default 405 Method Not Allowed responses #4478

@hellocoldworld

Description

@hellocoldworld

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.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


client = TestClient(app)


def test_method_not_allowed_has_allow_headers():
    response = client.put("/")
    assert response.status_code == 405, response.status_code
    assert "allow" in response.headers, response.headers

Description

A 405 Method Not Allowed response should include an 'Allow' header indicating what mehtods are allowed (ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405)
This was already pointed out in #1773 (comment) but since that issue and the related PR's seems stalled, I thought it could be good opening a dedicated issue.

Wanted Solution

The 'allow' header should be included with a list of allowed methods

Wanted Code

from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


client = TestClient(app)


def test_method_not_allowed_has_allow_headers():
    response = client.put("/")
    assert response.status_code == 405, response.status_code
    assert "allow" in response.headers, response.headers
    assert response.headers["allow"] == "GET, HEAD"

Alternatives

No response

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.70.1

Python Version

Python 3.9.6

Additional Context

No response

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