From 582a8642923d053308e7d685151ce691b60e833c Mon Sep 17 00:00:00 2001 From: jqualls Date: Sun, 12 Feb 2023 10:56:06 -0700 Subject: [PATCH] chore: add bind cookie to documentation --- docs/doc.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/doc.md b/docs/doc.md index 7cebab56..d0efcc40 100644 --- a/docs/doc.md +++ b/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)