Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Allow header in 405 Method Not Allowed response #446

Closed
alexedwards opened this issue Aug 20, 2019 · 8 comments · Fixed by #776
Closed

Include Allow header in 405 Method Not Allowed response #446

alexedwards opened this issue Aug 20, 2019 · 8 comments · Fixed by #776

Comments

@alexedwards
Copy link

alexedwards commented Aug 20, 2019

At the moment the default methodNotAllowedHandler doesn't include an Allow header containing the resource's supported methods.

I've made a minimal runnable example here: https://play.golang.org/p/CSUjlf8e30S

According to the HTTP RFC the Allow header must be included in 405 responses, so it would be good if there is a way that chi could support this.

bernardopericacho pushed a commit to bernardopericacho/chi that referenced this issue Nov 19, 2019
bernardopericacho pushed a commit to bernardopericacho/chi that referenced this issue Nov 19, 2019
bernardopericacho pushed a commit to bernardopericacho/chi that referenced this issue Nov 19, 2019
@pkieltyka
Copy link
Member

I'd recommend to implement this kind of behavior as a middleware where it will test for the route by accessing the router directly, and if not found, you can include the methods which are supported. As much as the RFC is cool, I don't see that this should be a "requirement"

@alexedwards
Copy link
Author

you can include the methods which are supported

@pkieltyka I've looked through the docs but I can't see how to get this information from the router. Could you point me in the right direction? Or do you mean that the middleware would need to contain its own secondary mapping of paths to available HTTP methods to perform the lookup?

@jeffwidman
Copy link

nudge @pkieltyka, I'm also interested in answers to ☝️.

@pkieltyka
Copy link
Member

your middleware could function similarly to GetHead where it would do a quick-pass to search the router for the method, serve if found, otherwise compile a list of methods on the route which are available instead and handle the response

https://github.com/go-chi/chi/blob/master/middleware/get_head.go#L24-L35 for how to search the router in a middleware

@alexedwards
Copy link
Author

alexedwards commented Jan 30, 2020

otherwise compile a list of methods on the route which are available instead

@pkieltyka It's this part that I don't get. It sounds like we would need to maintain a separate listing of all routes and their available methods. Is that what you're suggesting, or do I misunderstand?

I'm happy to dig into the code to see if there's a way to achieve this without making any breaking changes 🙂 But I don't want to waste the time on that if there's no desire to fix the issue?

@pkieltyka
Copy link
Member

just curious, but why are you so keen on this feature?

@pkieltyka
Copy link
Member

@alexedwards it is possible, see the GetHead middleware, its all there for you if you desire. Jump into the code and play around

@RadhikaHBhatt
Copy link

I am interested in this too. Currently, Is there another method that returns 405 along with allow headers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants