mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 05:42:09 +08:00
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:
parent
a331dc6a31
commit
c900ec70d4
23
README.md
23
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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user