mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
Merge 7e982b25b9312f0c326e25c8138ddcebaf3523f6 into d4e413648824333726ef65de5defc457e9dbf095
This commit is contained in:
commit
8e8b2cc355
@ -85,6 +85,7 @@ var (
|
|||||||
Uri = uriBinding{}
|
Uri = uriBinding{}
|
||||||
Header = headerBinding{}
|
Header = headerBinding{}
|
||||||
TOML = tomlBinding{}
|
TOML = tomlBinding{}
|
||||||
|
Cookie = cookieBinding{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Default returns the appropriate Binding instance based on the HTTP method
|
// Default returns the appropriate Binding instance based on the HTTP method
|
||||||
|
22
binding/cookie.go
Normal file
22
binding/cookie.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package binding
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type cookieBinding struct{}
|
||||||
|
|
||||||
|
func (cookieBinding) Name() string {
|
||||||
|
return "cookie"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c cookieBinding) Bind(req *http.Request, obj any) error {
|
||||||
|
cookies := make(map[string][]string, len(req.Cookies()))
|
||||||
|
for _, cookie := range req.Cookies() {
|
||||||
|
cookies[cookie.Name] = append(cookies[cookie.Name], cookie.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapFormByTag(obj, cookies, c.Name())
|
||||||
|
}
|
@ -737,6 +737,11 @@ func (c *Context) ShouldBindHeader(obj any) error {
|
|||||||
return c.ShouldBindWith(obj, binding.Header)
|
return c.ShouldBindWith(obj, binding.Header)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShouldBindCookie is a shortcut for c.ShouldBindWith(obj, binding.Cookie).
|
||||||
|
func (c *Context) ShouldBindCookie(obj any) error {
|
||||||
|
return c.ShouldBindWith(obj, binding.Cookie)
|
||||||
|
}
|
||||||
|
|
||||||
// ShouldBindUri binds the passed struct pointer using the specified binding engine.
|
// ShouldBindUri binds the passed struct pointer using the specified binding engine.
|
||||||
func (c *Context) ShouldBindUri(obj any) error {
|
func (c *Context) ShouldBindUri(obj any) error {
|
||||||
m := make(map[string][]string)
|
m := make(map[string][]string)
|
||||||
|
@ -1825,6 +1825,27 @@ func TestContextShouldBindHeader(t *testing.T) {
|
|||||||
assert.Equal(t, 0, w.Body.Len())
|
assert.Equal(t, 0, w.Body.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextShouldBindCookie(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
|
||||||
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||||
|
c.Request.AddCookie(&http.Cookie{Name: "rate", Value: "8000"})
|
||||||
|
c.Request.AddCookie(&http.Cookie{Name: "domain", Value: "music"})
|
||||||
|
c.Request.AddCookie(&http.Cookie{Name: "limit", Value: "1000"})
|
||||||
|
|
||||||
|
var testCookie struct {
|
||||||
|
Rate int `cookie:"rate"`
|
||||||
|
Domain string `cookie:"domain"`
|
||||||
|
Limit int `cookie:"limit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, c.ShouldBindCookie(&testCookie))
|
||||||
|
assert.Equal(t, 8000, testCookie.Rate)
|
||||||
|
assert.Equal(t, "music", testCookie.Domain)
|
||||||
|
assert.Equal(t, 1000, testCookie.Limit)
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextShouldBindWithQuery(t *testing.T) {
|
func TestContextShouldBindWithQuery(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
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 Query String or Post Data](#bind-query-string-or-post-data)
|
||||||
- [Bind Uri](#bind-uri)
|
- [Bind Uri](#bind-uri)
|
||||||
- [Bind Header](#bind-header)
|
- [Bind Header](#bind-header)
|
||||||
|
- [Bind Cookie](#bind-cookie)
|
||||||
- [Bind HTML checkboxes](#bind-html-checkboxes)
|
- [Bind HTML checkboxes](#bind-html-checkboxes)
|
||||||
- [Multipart/Urlencoded binding](#multiparturlencoded-binding)
|
- [Multipart/Urlencoded binding](#multiparturlencoded-binding)
|
||||||
- [XML, JSON, YAML, TOML and ProtoBuf rendering](#xml-json-yaml-toml-and-protobuf-rendering)
|
- [XML, JSON, YAML, TOML and ProtoBuf rendering](#xml-json-yaml-toml-and-protobuf-rendering)
|
||||||
@ -938,6 +939,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
|
### Bind HTML checkboxes
|
||||||
|
|
||||||
See the [detail information](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092)
|
See the [detail information](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user