Comments (2)
When you use HTTP methods, consider the security aspects. For tighter security, some firewalls do not allow HTTP PUT or DELETE traffic.
f you're using a server that can't handle PUT or DELETE traffic, you should use the query or head argument logic in func(c *gin.Context).
The following code has been uploaded to the code repository
// HandleContext re-enters a context that has been rewritten.
// This can be done by setting c.Request.URL.Path to your new target.
// Disclaimer: You can loop yourself to deal with this, use wisely.
func (engine *Engine) HandleContext(c *Context) {
oldIndexValue := c.index
c.reset()
engine.handleHTTPRequest(c)
c.index = oldIndexValue
}
package main
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func Method(r *gin.Engine) gin.HandlerFunc {
return func(c *gin.Context) {
if _method, ok := c.GetQuery("_method"); ok && strings.ToUpper(_method) != c.Request.Method {
c.Request.Method = strings.ToUpper(_method)
r.HandleContext(c)
c.Abort()
} else if _header := c.GetHeader("X-HTTP-Method-Override"); _header != "" && strings.ToUpper(_header) != c.Request.Method {
c.Request.Method = strings.ToUpper(_header)
r.HandleContext(c)
c.Abort()
}else{
c.Next()
}
}
}
func main() {
r := gin.Default()
r.Use(Method(r))
r.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK,gin.H{"message":"GET"})
})
r.POST("/test", func(c *gin.Context) {
c.JSON(http.StatusOK,gin.H{"message":"POST"})
})
r.PUT("/test", func(c *gin.Context) {
c.JSON(http.StatusOK,gin.H{"message":"PUT"})
})
if err := r.Run(":8080"); err != nil {
panic(err)
}
}
from gin.
c.Request.Method = strings.ToUpper(_method)
engine.HandleContext(c)
c.Abort()
from gin.
Related Issues (20)
- Will it be made into a more comprehensive framework later on? HOT 4
- How does the gin close the log file? HOT 3
- Should we use url.PathUnescape instead of url.QueryUnescape when UnescapePathValues is true
- go get failed: The requested name is valid, but no data of the requested type was found HOT 1
- 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
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.