Coder Social home page Coder Social logo

Comments (2)

RedCrazyGhost avatar RedCrazyGhost commented on May 21, 2024 1

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.

snowdream avatar snowdream commented on May 21, 2024
 	c.Request.Method = strings.ToUpper(_method)
			engine.HandleContext(c)
			c.Abort()

from gin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.