mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
chore: add bind cookie to documentation
This commit is contained in:
parent
48c56178db
commit
582a864292
40
docs/doc.md
40
docs/doc.md
@ -28,6 +28,7 @@
|
||||
- [Bind Query String or Post Data](#bind-query-string-or-post-data)
|
||||
- [Bind Uri](#bind-uri)
|
||||
- [Bind Header](#bind-header)
|
||||
- [Bind Cookie](#bind-cookie)
|
||||
- [Bind HTML checkboxes](#bind-html-checkboxes)
|
||||
- [Multipart/Urlencoded binding](#multiparturlencoded-binding)
|
||||
- [XML, JSON, YAML, TOML and ProtoBuf rendering](#xml-json-yaml-toml-and-protobuf-rendering)
|
||||
@ -900,6 +901,45 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### Bind Cookie
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type testCookie struct {
|
||||
Rate int `cookie:"Rate"`
|
||||
Domain string `cookie:"Domain"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
h := testCookie{}
|
||||
|
||||
if err := c.ShouldBindCookie(&h); err != nil {
|
||||
c.JSON(http.StatusOK, err)
|
||||
}
|
||||
|
||||
fmt.Printf("%#v\n", h)
|
||||
c.JSON(http.StatusOK, gin.H{"Rate": h.Rate, "Domain": h.Domain})
|
||||
})
|
||||
|
||||
r.Run()
|
||||
|
||||
// client
|
||||
// curl -H "rate:300" -H "domain:music" 127.0.0.1:8080/
|
||||
// output
|
||||
// {"Domain":"music","Rate":300}
|
||||
}
|
||||
```
|
||||
|
||||
### Bind HTML checkboxes
|
||||
|
||||
See the [detail information](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092)
|
||||
|
Loading…
x
Reference in New Issue
Block a user