模型绑定和验证
最后更新于:2022-04-02 07:26:31
若要将请求主体绑定到结构体中,请使用模型绑定,目前支持JSON、XML、YAML和标准表单值(foo=bar&boo=baz)的绑定。
Gin使用 [go-playground/validator.v8](https://github.com/go-playground/validator) 验证参数,[查看完整文档](https://godoc.org/gopkg.in/go-playground/validator.v8#hdr-Baked_In_Validators_and_Tags)。
需要在绑定的字段上设置tag,比如,绑定格式为json,需要这样设置 `json:"fieldname"` 。
此外,Gin还提供了两套绑定方法:
- Must bind
- - Methods - `Bind`, `BindJSON`, `BindXML`, `BindQuery`, `BindYAML`
- - Behavior - 这些方法底层使用 `MustBindWith`,如果存在绑定错误,请求将被以下指令中止 `c.AbortWithError(400, err).SetType(ErrorTypeBind)`,响应状态代码会被设置为400,请求头`Content-Type`被设置为`text/plain; charset=utf-8`。注意,如果你试图在此之后设置响应代码,将会发出一个警告 `[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422`,如果你希望更好地控制行为,请使用`ShouldBind`相关的方法
- Should bind
- - Methods - `ShouldBind`, `ShouldBindJSON`, `ShouldBindXML`, `ShouldBindQuery`, `ShouldBindYAML`
- - Behavior - 这些方法底层使用 `ShouldBindWith`,如果存在绑定错误,则返回错误,开发人员可以正确处理请求和错误。
当我们使用绑定方法时,Gin会根据Content-Type推断出使用哪种绑定器,如果你确定你绑定的是什么,你可以使用`MustBindWith`或者`BindingWith`。
你还可以给字段指定特定规则的修饰符,如果一个字段用`binding:"required"`修饰,并且在绑定时该字段的值为空,那么将返回一个错误。
```
// 绑定为json
type Login struct {
User string `form:"user" json:"user" xml:"user" binding:"required"`
Password string `form:"password" json:"password" xml:"password" binding:"required"`
}
func main() {
router := gin.Default()
// Example for binding JSON ({"user": "manu", "password": "123"})
router.POST("/loginJSON", func(c *gin.Context) {
var json Login
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if json.User != "manu" || json.Password != "123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
})
// Example for binding XML (
//
//
// user
// 123
// )
router.POST("/loginXML", func(c *gin.Context) {
var xml Login
if err := c.ShouldBindXML(&xml); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if xml.User != "manu" || xml.Password != "123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
})
// Example for binding a HTML form (user=manu&password=123)
router.POST("/loginForm", func(c *gin.Context) {
var form Login
// This will infer what binder to use depending on the content-type header.
if err := c.ShouldBind(&form); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if form.User != "manu" || form.Password != "123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
})
// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
}
```
**请求示例:**
```
$ curl -v -X POST \
http://localhost:8080/loginJSON \
-H 'content-type: application/json' \
-d '{ "user": "manu" }'
> POST /loginJSON HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.51.0
> Accept: */*
> content-type: application/json
> Content-Length: 18
>
* upload completely sent off: 18 out of 18 bytes
< HTTP/1.1 400 Bad Request
< Content-Type: application/json; charset=utf-8
< Date: Fri, 04 Aug 2017 03:51:31 GMT
< Content-Length: 100
<
{"error":"Key: 'Login.Password' Error:Field validation for 'Password' failed on the 'required' tag"}
```
**跳过验证:**
当使用上面的curl命令运行上面的示例时,返回错误,因为示例中`Password`字段使用了`binding:"required"`,如果我们使用`binding:"-"`,那么它就不会报错。
';