英文:
Go Gin - http "HEAD" request method
问题
我尝试在中间件中将传入请求的方法设置为"GET",如果请求方法是"HEAD",代码如下所示。
看起来,如果我使用curl -I命令,Gin会将其识别为"GET"请求,但是根据附加的日志显示(底部的日志),它会响应404。
我只是想看看是否可以在路由器级别上实现"HEAD"方法之外的方法来解决这个问题。有什么建议吗?
func CORS() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "*")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
// 如果传入请求是"HEAD",则将HTTP方法设置为"GET"
if c.Request.Method == "HEAD" {
c.Request.Method = "GET"
}
c.Next()
}
}
英文:
I tried to set http method to "GET" if an incoming request method is "HEAD" in a middleware like below.
It looks like Gin recognizes this as "GET" request if I do curl -I,
but it responds with 404 as the attached log shows (the bottom one).
I just wanted to see if this works without implementing "HEAD" method in a router level.
Any advice?
func CORS() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "*")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
// set http method to "GET" if an incoming request is "HEAD"
if c.Request.Method == "HEAD" {
c.Request.Method = "GET"
}
c.Next()
}
}
答案1
得分: 0
因为gin中间件是在路由匹配之后获取的处理函数,所以中间件在路由匹配之后执行,因此中间件不能修改路由匹配方法。
通过对http.Handler(gin.Engine)进行装饰,使用http.Server中间件来修改请求方法。
注意:'HEAD'请求没有响应体。如果将'HEAD'转换为'GET'后有响应体,这不符合HTTP协议规范,建议将所有'Get'处理函数注册为'Head'方法。
func main() {
router := gin.Default()
s := &http.Server{
Addr: ":8080",
Handler: http.Handler(func(w http.ResponseWriter, r *http.Request){
if r.Method == "HEAD" {
r.Method = "GET"
}
router.ServeHTTP(w, r)
}),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}
英文:
Because gin middleware is a processing function obtained after route matching, middleware executes after route matching, so middleware cannot modify the route matching method.
By decorating a layer of http.Handler(gin.Engine), use http.Server middleware to modify the request method.
Note: There is no response body for the 'HEAD' request. If there is a response body after converting 'HEAD' to 'GET', which does not conform to the http protocol specification, it is recommended to register all the 'Get' processing functions with the 'Head' method.
func main() {
router := gin.Default()
s := &http.Server{
Addr: ":8080",
Handler: http.Handler(func(w http.ResponseWriter, r *http.Request){
if r.Method == "HEAD" {
r.Method = "GET"
}
router.ServeHTTP(w, r)
}),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论