From 027f84632a1fb67b395ae72e69a06b4c26f1696b Mon Sep 17 00:00:00 2001 From: guonaihong Date: Tue, 18 Jun 2019 19:29:45 +0800 Subject: [PATCH] support bind http header param #1956 update #1956 ``` package main import ( "fmt" "github.com/gin-gonic/gin" ) type testHeader struct { Rate int `header:"Rate"` Domain string `header:"Domain"` } func main() { r := gin.Default() r.GET("/", func(c *gin.Context) { h := testHeader{} if err := c.ShouldBindHeader(&h); err != nil { c.JSON(200, err) } fmt.Printf("%#v\n", h) c.JSON(200, 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} } ``` --- binding/binding.go | 1 + binding/form_mapping.go | 4 ++++ binding/header.go | 18 ++++++++++++++++++ context.go | 10 ++++++++++ 4 files changed, 33 insertions(+) create mode 100644 binding/header.go diff --git a/binding/binding.go b/binding/binding.go index 520c510976..6d58c3cdd1 100644 --- a/binding/binding.go +++ b/binding/binding.go @@ -78,6 +78,7 @@ var ( MsgPack = msgpackBinding{} YAML = yamlBinding{} Uri = uriBinding{} + Header = headerBinding{} ) // Default returns the appropriate Binding instance based on the HTTP method diff --git a/binding/form_mapping.go b/binding/form_mapping.go index ebf3b19924..7b84e2ea96 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -21,6 +21,10 @@ func mapUri(ptr interface{}, m map[string][]string) error { return mapFormByTag(ptr, m, "uri") } +func mapHeader(ptr interface{}, m map[string][]string) error { + return mapFormByTag(ptr, m, "header") +} + func mapForm(ptr interface{}, form map[string][]string) error { return mapFormByTag(ptr, form, "form") } diff --git a/binding/header.go b/binding/header.go new file mode 100644 index 0000000000..0f9e5fd489 --- /dev/null +++ b/binding/header.go @@ -0,0 +1,18 @@ +package binding + +import "net/http" + +type headerBinding struct{} + +func (headerBinding) Name() string { + return "header" +} + +func (headerBinding) Bind(req *http.Request, obj interface{}) error { + + if err := mapHeader(obj, req.Header); err != nil { + return err + } + + return validate(obj) +} diff --git a/context.go b/context.go index 77cdc182aa..d9fcc28596 100644 --- a/context.go +++ b/context.go @@ -583,6 +583,11 @@ func (c *Context) BindYAML(obj interface{}) error { return c.MustBindWith(obj, binding.YAML) } +// BindHeader is a shortcut for c.MustBindWith(obj, binding.Header). +func (c *Context) BindHeader(obj interface{}) error { + return c.MustBindWith(obj, binding.Header) +} + // BindUri binds the passed struct pointer using binding.Uri. // It will abort the request with HTTP 400 if any error occurs. func (c *Context) BindUri(obj interface{}) error { @@ -637,6 +642,11 @@ func (c *Context) ShouldBindYAML(obj interface{}) error { return c.ShouldBindWith(obj, binding.YAML) } +// ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, binding.Header). +func (c *Context) ShouldBindHeader(obj interface{}) error { + return c.ShouldBindWith(obj, binding.Header) +} + // ShouldBindUri binds the passed struct pointer using the specified binding engine. func (c *Context) ShouldBindUri(obj interface{}) error { m := make(map[string][]string)