feat(bind): ShouldBindBodyWith shortcut and change doc (#3871)

* feat: ShouldBindBodyWith shortcut and change doc

* fix: yaml can parse json test case

* style: fix new test case in context_test.go

* chore: modify the code style to specify binding type

* chroe: gofmt modifies the code format
This commit is contained in:
RedCrazyGhost
2024-03-23 22:09:02 +08:00
committed by GitHub
parent fd1faaded0
commit 7a865dcf1d
3 changed files with 284 additions and 2 deletions

View File

@ -1956,7 +1956,12 @@ func SomeHandler(c *gin.Context) {
}
```
For this, you can use `c.ShouldBindBodyWith`.
For this, you can use `c.ShouldBindBodyWith` or shortcuts.
- `c.ShouldBindBodyWithJSON` is a shortcut for c.ShouldBindBodyWith(obj, binding.JSON).
- `c.ShouldBindBodyWithXML` is a shortcut for c.ShouldBindBodyWith(obj, binding.XML).
- `c.ShouldBindBodyWithYAML` is a shortcut for c.ShouldBindBodyWith(obj, binding.YAML).
- `c.ShouldBindBodyWithTOML` is a shortcut for c.ShouldBindBodyWith(obj, binding.TOML).
```go
func SomeHandler(c *gin.Context) {
@ -1969,7 +1974,7 @@ func SomeHandler(c *gin.Context) {
} else if errB := c.ShouldBindBodyWith(&objB, binding.JSON); errB == nil {
c.String(http.StatusOK, `the body should be formB JSON`)
// And it can accepts other formats
} else if errB2 := c.ShouldBindBodyWith(&objB, binding.XML); errB2 == nil {
} else if errB2 := c.ShouldBindBodyWithXML(&objB); errB2 == nil {
c.String(http.StatusOK, `the body should be formB XML`)
} else {
...