``` go
func main() {
r := gin.Default()
// r.GET("/JSONP?callback=x", func(c *gin.Context) { // old
r.GET("/JSONP", func(c *gin.Context) { // new
data := gin.H{
"foo": "bar",
}
//callback is x
// Will output : x({\"foo\":\"bar\"})
c.JSONP(http.StatusOK, data)
})
// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}
// client
// curl http://127.0.0.1:8080/JSONP?callback=x
// old output
// 404 page not found
// new output
// x({"foo":"bar"})
```
Most of the sample code in the documentation map[string]interface{} is represented by gin.H.
gin.H is a very important place for me to like gin, can write a lot less code
* 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}
}
```
* add unit test
* Modify the code to get the http header
When the http header is obtained in the standard library,
the key value will be modified by the CanonicalMIMEHeaderKey function,
and finally the value of the http header will be obtained from the map.
As follows.
```go
func (h MIMEHeader) Get(key string) string {
// ...
v := h[CanonicalMIMEHeaderKey(key)]
// ...
}
```
This pr also follows this modification
* Thanks to vkd for suggestions, modifying code
* Increase test coverage
env GOPATH=`pwd` go test github.com/gin-gonic/gin/binding -coverprofile=cover.prof
ok github.com/gin-gonic/gin/binding 0.015s coverage: 100.0% of statements
* Rollback check code
* add use case to README.md
* Fix context.Params race condition on Copy()
Using context.Param(key) on a context.Copy inside a goroutine
may lead to incorrect value on a high load, where another request
overwrite a Param
* Using waitgroup to wait asynchronous test case
* move tests of mapping to separate test file
make 100% coverage of form_mapping.go from form_mapping_test.go file
* fix tests for go 1.6
go 1.6 doesn't support `t.Run(...)` subtests
* refactor(form_mapping.go): mapping multipart request
* add checkers for a types to match with the setter interface
* form_mapping.go: rename method name on setter interface, add comments
* fix style of comments
Fix: https://github.com/gin-gonic/gin/issues/1804
`allNoRoute` contains middlewares such as `gin.Logger`, `gin.Recovery`, so on. The correct code is to use `noRoute`.
cc: @MetalBreaker
* refactor(form_mapping.go): mapping ptr, struct and map
* fix#1672 correct work with ptr - not create value if field is not set
* avoid allocations on strings.Split() - change to strings.Index()
* fix#610 tag value "-" is mean ignoring field
* struct fields mapped like json.Unmarshal
* map fields mapped like json.Unmarshal
* fix after @thinkerou review