要將請求體綁定到結構體中,使用模型綁定。 Gin目前支持JSON、XML、YAML和標準表單值的綁定(foo=bar&boo=baz)。
Gin使用 [**go-playground/validator.v8**](https://github.com/go-playground/validator) 進行驗證。 查看標簽用法的全部[文檔](http://godoc.org/gopkg.in/go-playground/validator.v8#hdr-Baked_In_Validators_and_Tags).
使用時,需要在要綁定的所有字段上,設置相應的tag。 例如,使用 JSON 綁定時,設置字段標簽為 `json:"fieldname"`。
Gin提供了兩類綁定方法:
- **Type** - 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會輸出日志 `[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422`。 如果您希望更好地控制綁定,考慮使用 `ShouldBind` 等效方法。
- **Type** - Should bind
- **Methods** - `ShouldBind`, `ShouldBindJSON`, `ShouldBindXML`, `ShouldBindQuery`, `ShouldBindYAML`
- **Behavior** - 這些方法屬于 `ShouldBindWith` 的具體調用。 如果發生綁定錯誤,Gin 會返回錯誤并由開發者處理錯誤和請求。
使用 Bind 方法時,Gin 會嘗試根據 Content-Type 推斷如何綁定。 如果你明確知道要綁定什么,可以使用 `MustBindWith` 或 `ShouldBindWith`。
你也可以指定必須綁定的字段。 如果一個字段的 tag 加上了 `binding:"required"`,但綁定時是空值, Gin 會報錯。
```go
// 綁定 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()
// 綁定 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"})
})
// 綁定 XML (
// <?xml version="1.0" encoding="UTF-8"?>
// <root>
// <user>user</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"})
})
// 綁定 HTML 表單 (user=manu&password=123)
router.POST("/loginForm", func(c *gin.Context) {
var form Login
// 根據 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"})
})
// 監聽并在 0.0.0.0:8080 上啟動服務
router.Run(":8080")
}
```
#### 示例請求
```sh
$ 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:"-"`, 再次運行上面的示例就不會返回錯誤。
- 介紹
- 快速入門
- 基準測試
- 特性
- Jsoniter
- 示例
- AsciiJSON
- HTML 渲染
- HTTP2 server 推送
- JSONP
- Multipart/Urlencoded 綁定
- Multipart/Urlencoded 表單
- PureJSON
- Query 和 post form
- SecureJSON
- XML/JSON/YAML/ProtoBuf 渲染
- 上傳文件
- 單文件
- 多文件
- 不使用默認的中間件
- 從 reader 讀取數據
- 優雅地重啟或停止
- 使用 BasicAuth 中間件
- 使用 HTTP 方法
- 使用中間件
- 只綁定 url 查詢字符串
- 在中間件中使用 Goroutine
- 多模板
- 如何記錄日志
- 定義路由日志的格式
- 將 request body 綁定到不同的結構體中
- 支持 Let's Encrypt
- 映射查詢字符串或表單參數
- 查詢字符串參數
- 模型綁定和驗證
- 綁定 HTML 復選框
- 綁定 Uri
- 綁定查詢字符串或表單數據
- 綁定表單數據至自定義結構體
- 自定義 HTTP 配置
- 自定義中間件
- 自定義驗證器
- 設置和獲取 Cookie
- 路由參數
- 路由組
- 運行多個服務
- 重定向
- 靜態文件服務
- 靜態資源嵌入
- 測試
- 用戶
- FAQ