Search code examples
gogo-gin

How do I find the request method from Go Gin context?


I want to use the same handler for multiple endpoints in a Go Gin app:

    router.POST("/box/:boxID", controllers.AddUpdateBox)
    router.PUT("/box/:boxID", controllers.AddUpdateBox)
    router.PATCH("/box/:boxID", controllers.AddUpdateBox)

In the controller I want to find what is the request method (POST, PUT or PATCH).

func AddUpdateBox(c *gin.Context) {
  
}

How do I get the request method from the Gin context?


Solution

  • c.Request.Method
    

    This member represents the method of the request