模型绑定和验证

要将请求主体绑定到类型,请使用模型绑定。我们目前支持 JSON、XML、YAML 和标准表单值(foo=bar&boo=baz)的绑定。

Gin 使用 go-playground/validator/v10 进行验证。在此处查看有关标签用法的完整文档 here

请注意,您需要在要绑定的所有字段上设置相应的绑定标记。例如,从 JSON 绑定时,设置 json:"fieldname"

此外,Gin 提供了两组绑定方法

  • 类型 - 必须绑定
    • 方法 - BindBindJSONBindXMLBindQueryBindYAML
    • 行为 - 这些方法在底层使用 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 方法。
  • 类型 - 应该绑定
    • 方法 - ShouldBindShouldBindJSONShouldBindXMLShouldBindQueryShouldBindYAML
    • 行为 - 这些方法在底层使用 ShouldBindWith。如果存在绑定错误,则返回错误,并且由开发人员负责适当处理请求和错误。

在使用 Bind 方法时,Gin 会尝试根据 Content-Type 标头推断绑定器。如果您确定要绑定什么,则可以使用 MustBindWithShouldBindWith

您还可以指定特定字段是必需的。如果某个字段用 binding:"required" 修饰,并且在绑定时具有空值,则会返回错误。

// Binding from 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 (
	//	<?xml version="1.0" encoding="UTF-8"?>
	//	<root>
	//		<user>manu</user>
	//		<password>123</password>
	//	</root>)
	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"。如果对 Password 使用 binding:"-",则再次运行上述示例时不会返回错误。

上次修改时间:2024 年 5 月 10 日:Bump GitHub action workflows (#276) (4371021)