diff --git a/README.md b/README.md index 119f9452..34847291 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,29 @@ func main() { } ``` +### Getting Request Method + +```go +package main + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +func main() { + r := gin.Default() + + r.GET("/", func(c *gin.Context) { + c.String(http.StatusOK, fmt.Sprintf("This is a %s request.", c.Method())) + }) + + r.Run() +} +``` + ### Parameters in path ```go diff --git a/context.go b/context.go index 3d6b56d6..d626f682 100644 --- a/context.go +++ b/context.go @@ -768,6 +768,15 @@ func (c *Context) IsWebsocket() bool { return false } +// Method returns the HTTP method of the requests. +func (c *Context) Method() string { + method := c.Request.Method + if method == "" { + return http.MethodGet + } + return method +} + func (c *Context) requestHeader(key string) string { return c.Request.Header.Get(key) }