-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Support for 405 Method Not Allowed #20
Comments
What kind of support would you like? Right you could call: func(c *gin.Context) {
c.String(405, "method not allowed")
// or even better: c.Fail(405, "method not allowed")
// or both! ha
} We are already working to improve logging possibilities, check this out: #16 |
Thanks a lot Manu! But lets say I have:
How do I implement so all other methods to /services except GET responds with 405? And great to see that you are improving the logging possibilities :) |
I guess it's a little bit hacky, but you can use:
Definitely, now I want to include good support for 405, but since it's a routing issue, I am going to ask the maintainer of HttpRouter. It will print: "http: multiple response.WriteHeader calls" |
Muchas Gracias Manu! Its true it looks like a bit evil hack for now, and hopefully there will later on be greater support. 👍 Looking forward to see how Gin develops :) |
@manucorporat Given httprouter's response to the original request, it seems like this should be handled at the Gin end. I wrote a middleware adding support for 405 Method Not Allowed (with the appropriate "Accept" header and everything). It intercepts 404 responses and intelligently converts them to 405s if applicable. In order to shoehorn this into contrib, it would require exposing httprouter or passing through its Router Lookup method. // Pass through httprouter Lookup.
// Lookup allows the manual lookup of a method + path combo, for more powerful middleware and such.
func (engine *Engine) Lookup(method, path string) (httprouter.Handle, httprouter.Params, bool) {
return engine.router.Lookup(method, path)
} The middleware could also go into Gin itself rather than contrib, and Gin's httprouter instance could be directly injected into the middleware on initialization. This would make it so only Gin could add the middleware, though, so it would be an Engine option rather than a composable middleware... kind of like the "trailing slash redirect" option in httprouter. I'd lean more toward this approach, and even enable it by default, but I'm not sure if that's in line with Gin's philosophy. So where should I send the pull request? One to contrib with the middleware plus one to gin exposing httprouter Lookup, or just one pull request to Gin with the more integrated approach? |
Sorry for delay. Support for HTTP 405 has been added julienschmidt/httprouter#51 . Closing this issue now. |
* ci: add github action workflows * test: fixed the TestUnixSocket test on windows (#20) * ci: add github action workflows (#18) * Remove .travis.yml * ci: replace GITTER_ROOM_ID and upload coverage every time you go test * ci: update coverage using codecov/codecov-action@v1 * Merge branch 'master' into github-actions * repo: replace travis ci to github actions * ci: add go version 1.16 * fix: go install requires a specific version * chore(ci): remove go 1.12 support * chore(ci): remove os windows-latest Co-authored-by: thinkerou <thinkerou@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
…2751) * Update tree.go (#2659) delete more "()" * updated comments for Get function for params (#2756) * ci: add github action workflows (#2596) * ci: add github action workflows * test: fixed the TestUnixSocket test on windows (#20) * ci: add github action workflows (#18) * Remove .travis.yml * ci: replace GITTER_ROOM_ID and upload coverage every time you go test * ci: update coverage using codecov/codecov-action@v1 * Merge branch 'master' into github-actions * repo: replace travis ci to github actions * ci: add go version 1.16 * fix: go install requires a specific version * chore(ci): remove go 1.12 support * chore(ci): remove os windows-latest Co-authored-by: thinkerou <thinkerou@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> * Setting trusted platform using an enum-like (#2739) * gin.Context with fallback value from c.Request.Context() * add test case Co-authored-by: youzeliang <youzel@126.com> Co-authored-by: Ashwani <ashwanisharma686@gmail.com> Co-authored-by: Jeff <laojianzi1994@gmail.com> Co-authored-by: thinkerou <thinkerou@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-authored-by: Alessandro (Ale) Segala <43508+ItalyPaleAle@users.noreply.github.com>
* ci: add github action workflows * test: fixed the TestUnixSocket test on windows (#20) * ci: add github action workflows (#18) * Remove .travis.yml * ci: replace GITTER_ROOM_ID and upload coverage every time you go test * ci: update coverage using codecov/codecov-action@v1 * Merge branch 'master' into github-actions * repo: replace travis ci to github actions * ci: add go version 1.16 * fix: go install requires a specific version * chore(ci): remove go 1.12 support * chore(ci): remove os windows-latest Co-authored-by: thinkerou <thinkerou@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Would be great to easy add support for status 405, specially when creating a REST API.
And do you have any example adding a more verbose access logger? Logging response-code, user-agent, client-ip, etc?
Keep up the good work! 👍
The text was updated successfully, but these errors were encountered: