feat: add Method() function to gin.Context

test: add test for gin.Context.Method() function

docs: add docs to README for gin.Context.Method() function

revert "docs: add docs to README for gin.Context.Method() function"

This reverts commit 0c32bf3c8f0aeaf3b159fc1691f4ab658bb9b05e.
This commit is contained in:
Leung Yau Ming 2021-03-31 10:57:47 +08:00
parent a331dc6a31
commit c900ec70d4
2 changed files with 32 additions and 0 deletions

View File

@ -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 ### Parameters in path
```go ```go

View File

@ -768,6 +768,15 @@ func (c *Context) IsWebsocket() bool {
return false 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 { func (c *Context) requestHeader(key string) string {
return c.Request.Header.Get(key) return c.Request.Header.Get(key)
} }