Comments (8)
Thanks a lot Manu!
But lets say I have:
v1.GET("/services", func(c *gin.Context) {
// do something
c.JSON(200, something)
})
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 :)
from gin.
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
but we wanted to implement it properly, that's why it is not in this release.
from gin.
I guess it's a little bit hacky, but you can use:
router.NotFound404(func(c *gin.Context) {
if c.Req.RequestURI == "/services" {
c.String(405, "Method Not Allowed")
}
})
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"
do not worry about that, it's not an important issue and we are about to fix that: #16
from gin.
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 :)
from gin.
See julienschmidt/httprouter#13
from gin.
@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?
from gin.
from gin.
Sorry for delay. Support for HTTP 405 has been added julienschmidt/httprouter#51 . Closing this issue now.
from gin.
Related Issues (20)
- Attach Response Headers (or middlewares) to redirected requests (CORS issues) HOT 1
- Should context.GetRawData() also copy body to BodyBytesKey? HOT 3
- YAML BindBody should not parse JSON content HOT 1
- Gin do not support javascript ? HOT 5
- Does gin.Recovery() run before or after Group middleware? HOT 1
- How can i prevent a http connection to close HOT 3
- BasicAuth is not suitable for proxy auth
- Can't get status as expected from context.Status() HOT 1
- Gin Context implementation breaks context.Context contract HOT 4
- [Suggestions] Introducing an AI-powered robot to assist with daily issue and pull request (PR) tasks.
- Need c.JSON() response like behaviour for c.SSEvent() response for supporting first party clients like OpenAI SDK HOT 2
- custom time.Time type can not use bindQuery get a value. HOT 3
- configure server parameters HOT 1
- The binding:"required" tag does not seem to work for struct types HOT 3
- Is there a way to preserve order in JSON on response? HOT 1
- redirect is not success HOT 1
- I want to use shouldmindbodywith first and then use formafile, but it doesn't work right now HOT 3
- Extend the routing tree entry
- Transparent decompression for gzip, deflate, etc? HOT 1
- Custom time.Time type can not use bindQuery get a value 2 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gin.