Add "Allow" headers to 405 Method Not Allowed responses#1434
Conversation
|
Could we instead add a For context, FastAPI already has a subclass that does this, so that PR would also upstream their customization: https://github.com/tiangolo/fastapi/blob/291180bf2d8c39e84860c2426b1d58b6c80f6fef/fastapi/exceptions.py#L13 |
|
The code will look faaaaar more clear with that. I've created #1435, as it's another issue. But maybe none of those two PRs are needed, as we need to solve the question I asked above:
Should we add a |
Default answer to this is nope, thanks. 😅 Let's not extend the API of Starlette at all if we can help it. |
| return PlainTextResponse( | ||
| "Method Not Allowed", | ||
| status_code=405, | ||
| headers={"allow": ", ".join(allowed_methods)}, |
There was a problem hiding this comment.
I guess we'd may as well use standard HTTP casing here - Allow.
(Eg. I checked https://github.com/encode/starlette/blob/master/starlette/middleware/gzip.py to see what we use internally elsewhere)
My thought was that once #1435 gets merged we'd just use that feature in Route.handle and be done with this. I don't think we need to automagically enforce this for any response with |
|
Replaced by #1436 |
This PR was inspired based on a FastAPI issue reported by @hellocoldworld.
As we can see on the RFC 7231, responses with 405 status code MUST provide the
allowheaders. Starlette doesn't honor that.What this PR introduces is a potential solution, which I'd like to discuss - to understand if it's the best solution. Changes here:
ExceptionMiddlewareaddsallowheaders whenHTTPException.status_code == 405. (I'm not sure if this makes sense yet).allowheaders whenPlainResponse.status_code == 405.There's still a case that is not being handled on this PR:
allowheaders. It should, right?Important resources: