type User struct { Name string`json:"name" form:"name" binding:"required"`// 必填 Age int`json:"age" form:"age" binding:"required"`// 必填 Home string`json:"home" form:"home" binding:"required"`// 必填 }
#1 三个字段都是必须要传的,只传一个会输出错误 $ curl "http://127.0.0.1:9090/bindGet/getMust?home=beijing" {"error":"Key: 'User.Name' Error:Field validation for 'Name' failed on the 'required' tag\nKey: 'User.Age' Error:Field validation for 'Age' failed on the 'required' tag\n"}%
{"error":"Key: 'User.Name' Error:Field validation for 'Name' failed on the 'required' tag\nKey: 'User.Age' Error:Field validation for 'Age' failed on the 'required' tag\n"}
#3 控制台输出 [GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 200 [GIN] 2023/03/26 - 20:32:45 | 200 | 87.344µs | 127.0.0.1 | GET "/bindGet/getMust?home=beijing"
#1 三个字段都是必须要传的,只传一个会输出错误 $ curl "http://127.0.0.1:9090/bindGet/getShould?home=beijing" {"error":"Key: 'User.Name' Error:Field validation for 'Name' failed on the 'required' tag\nKey: 'User.Age' Error:Field validation for 'Age' failed on the 'required' tag\n"}%
#2 同上一样的请求,看一下头信息 $ curl -i "http://127.0.0.1:9090/bindGet/getShould?home=beijing" HTTP/1.1200 OK Content-Type: application/json; charset=utf-8 Date: Sun, 26 Mar 202312:56:13 GMT Content-Length: 172
{"error":"Key: 'User.Name' Error:Field validation for 'Name' failed on the 'required' tag\nKey: 'User.Age' Error:Field validation for 'Age' failed on the 'required' tag\n"}
#1 访问数据 $ curl --request "POST" -H "content-type:application/json" -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.50" --url "http://127.0.0.1:9090/bind/header"
#1 返回数据 { "header": { "ContentType": "application/json", "UserAgenet": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.50" } }