Skip to content

Commit

Permalink
support bind http header param gin-gonic#1956
Browse files Browse the repository at this point in the history
update gin-gonic#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}
}
```
  • Loading branch information
guonaihong committed Jun 18, 2019
1 parent 75b9d2b commit 027f846
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var (
MsgPack = msgpackBinding{}
YAML = yamlBinding{}
Uri = uriBinding{}
Header = headerBinding{}
)

// Default returns the appropriate Binding instance based on the HTTP method
Expand Down
4 changes: 4 additions & 0 deletions binding/form_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
18 changes: 18 additions & 0 deletions binding/header.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 10 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 027f846

Please sign in to comment.