gin/binding
guonaihong 7e9f808e02 fix empty value error
Here is the code that can report an error
```go
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
	"os"
	"time"
)

type header struct {
	Duration   time.Duration `header:"duration"`
	CreateTime time.Time     `header:"createTime" time_format:"unix"`
}

func needFix1() {
	g := gin.Default()
	g.GET("/", func(c *gin.Context) {
		h := header{}
		err := c.ShouldBindHeader(&h)
		if err != nil {
			c.JSON(500, fmt.Sprintf("fail:%s\n", err))
			return
		}

		c.JSON(200, h)
	})

	g.Run(":8081")
}

func needFix2() {
	g := gin.Default()
	g.GET("/", func(c *gin.Context) {
		h := header{}
		err := c.ShouldBindHeader(&h)
		if err != nil {
			c.JSON(500, fmt.Sprintf("fail:%s\n", err))
			return
		}

		c.JSON(200, h)
	})

	g.Run(":8082")
}

func sendNeedFix1() {
	// send to needFix1
	sendBadData("http://127.0.0.1:8081", "duration")
}

func sendNeedFix2() {
	// send to needFix2
	sendBadData("http://127.0.0.1:8082", "createTime")
}

func sendBadData(url, key string) {
	req, err := http.NewRequest("GET", "http://127.0.0.1:8081", nil)
	if err != nil {
		fmt.Printf("err:%s\n", err)
		return
	}

	// Only the key and no value can cause an error
	req.Header.Add(key, "")
	rsp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}
	io.Copy(os.Stdout, rsp.Body)
	rsp.Body.Close()
}

func main() {
	go needFix1()
	go needFix2()

	time.Sleep(time.Second / 1000 * 200) // 200ms
	sendNeedFix1()
	sendNeedFix2()
}

```
2019-12-06 15:49:43 +08:00
..
binding.go use http method constant (#2155) 2019-11-29 07:50:49 +08:00
binding_test.go fix empty value error 2019-12-06 15:49:43 +08:00
default_validator.go upgrade go-validator to v10 (#2149) 2019-11-25 14:49:45 +08:00
form.go Refactor redirect request in gin.go (#1970) 2019-11-26 08:19:30 +08:00
form_mapping.go fix empty value error 2019-12-06 15:49:43 +08:00
form_mapping_benchmark_test.go Fix 'errcheck' linter warnings (#2093) 2019-10-27 13:58:59 +08:00
form_mapping_test.go fix ignore walking on form mapping (#1942) (#1943) 2019-11-01 10:47:40 +08:00
header.go support bind http header param #1956 (#1957) 2019-06-27 12:47:45 +08:00
json.go feat(binding): add DisallowUnknownFields() in gin.Context.BindJSON() (#2028) 2019-09-06 13:56:59 +08:00
json_test.go Relocate binding body tests (#2086) 2019-10-10 16:58:31 +08:00
msgpack.go Fix #216: Enable to call binding multiple times in some formats (#1341) 2018-05-11 10:33:33 +08:00
msgpack_test.go Relocate binding body tests (#2086) 2019-10-10 16:58:31 +08:00
multipart_form_mapping.go binding: add support of multipart multi files (#1878) (#1949) 2019-06-18 19:49:10 +08:00
multipart_form_mapping_test.go binding: add support of multipart multi files (#1878) (#1949) 2019-06-18 19:49:10 +08:00
protobuf.go Fix typos (#1626) 2018-11-05 14:17:04 +08:00
query.go style(import): not use aliase when import package (#1146) 2017-10-29 13:12:22 +01:00
uri.go support bind uri param (#1612) 2018-11-22 09:29:48 +08:00
validate_test.go upgrade go-validator to v10 (#2149) 2019-11-25 14:49:45 +08:00
xml.go Fix #216: Enable to call binding multiple times in some formats (#1341) 2018-05-11 10:33:33 +08:00
xml_test.go Relocate binding body tests (#2086) 2019-10-10 16:58:31 +08:00
yaml.go Yaml binding (#1618) 2018-11-06 09:49:45 +08:00
yaml_test.go Relocate binding body tests (#2086) 2019-10-10 16:58:31 +08:00