mirror of
https://github.com/gin-gonic/gin.git
synced 2025-12-03 21:44:33 +08:00
docs(context): add example comments for ShouldBind* methods (#4428)
- Added detailed example for ShouldBindJSON - Added consistent descriptive comments for ShouldBindXML, ShouldBindQuery, ShouldBindYAML, ShouldBindTOML, ShouldBindPlain, ShouldBindHeader, ShouldBindUri - Makes binding method usage clearer for new users
This commit is contained in:
parent
a85ef5ce4d
commit
58135f06cf
30
context.go
30
context.go
@ -830,41 +830,71 @@ func (c *Context) ShouldBind(obj any) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).
|
// ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// POST /user
|
||||||
|
// Content-Type: application/json
|
||||||
|
//
|
||||||
|
// Request Body:
|
||||||
|
// {
|
||||||
|
// "name": "Manu",
|
||||||
|
// "age": 20
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// type User struct {
|
||||||
|
// Name string `json:"name"`
|
||||||
|
// Age int `json:"age"`
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// var user User
|
||||||
|
// if err := c.ShouldBindJSON(&user); err != nil {
|
||||||
|
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// c.JSON(http.StatusOK, user)
|
||||||
func (c *Context) ShouldBindJSON(obj any) error {
|
func (c *Context) ShouldBindJSON(obj any) error {
|
||||||
return c.ShouldBindWith(obj, binding.JSON)
|
return c.ShouldBindWith(obj, binding.JSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindXML is a shortcut for c.ShouldBindWith(obj, binding.XML).
|
// ShouldBindXML is a shortcut for c.ShouldBindWith(obj, binding.XML).
|
||||||
|
// It works like ShouldBindJSON but binds the request body as XML data.
|
||||||
func (c *Context) ShouldBindXML(obj any) error {
|
func (c *Context) ShouldBindXML(obj any) error {
|
||||||
return c.ShouldBindWith(obj, binding.XML)
|
return c.ShouldBindWith(obj, binding.XML)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).
|
// ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).
|
||||||
|
// It works like ShouldBindJSON but binds query parameters from the URL.
|
||||||
func (c *Context) ShouldBindQuery(obj any) error {
|
func (c *Context) ShouldBindQuery(obj any) error {
|
||||||
return c.ShouldBindWith(obj, binding.Query)
|
return c.ShouldBindWith(obj, binding.Query)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindYAML is a shortcut for c.ShouldBindWith(obj, binding.YAML).
|
// ShouldBindYAML is a shortcut for c.ShouldBindWith(obj, binding.YAML).
|
||||||
|
// It works like ShouldBindJSON but binds the request body as YAML data.
|
||||||
func (c *Context) ShouldBindYAML(obj any) error {
|
func (c *Context) ShouldBindYAML(obj any) error {
|
||||||
return c.ShouldBindWith(obj, binding.YAML)
|
return c.ShouldBindWith(obj, binding.YAML)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindTOML is a shortcut for c.ShouldBindWith(obj, binding.TOML).
|
// ShouldBindTOML is a shortcut for c.ShouldBindWith(obj, binding.TOML).
|
||||||
|
// It works like ShouldBindJSON but binds the request body as TOML data.
|
||||||
func (c *Context) ShouldBindTOML(obj any) error {
|
func (c *Context) ShouldBindTOML(obj any) error {
|
||||||
return c.ShouldBindWith(obj, binding.TOML)
|
return c.ShouldBindWith(obj, binding.TOML)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindPlain is a shortcut for c.ShouldBindWith(obj, binding.Plain).
|
// ShouldBindPlain is a shortcut for c.ShouldBindWith(obj, binding.Plain).
|
||||||
|
// It works like ShouldBindJSON but binds plain text data from the request body.
|
||||||
func (c *Context) ShouldBindPlain(obj any) error {
|
func (c *Context) ShouldBindPlain(obj any) error {
|
||||||
return c.ShouldBindWith(obj, binding.Plain)
|
return c.ShouldBindWith(obj, binding.Plain)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, binding.Header).
|
// ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, binding.Header).
|
||||||
|
// It works like ShouldBindJSON but binds values from HTTP headers.
|
||||||
func (c *Context) ShouldBindHeader(obj any) error {
|
func (c *Context) ShouldBindHeader(obj any) error {
|
||||||
return c.ShouldBindWith(obj, binding.Header)
|
return c.ShouldBindWith(obj, binding.Header)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldBindUri binds the passed struct pointer using the specified binding engine.
|
// ShouldBindUri binds the passed struct pointer using the specified binding engine.
|
||||||
|
// It works like ShouldBindJSON but binds parameters from the URI.
|
||||||
func (c *Context) ShouldBindUri(obj any) error {
|
func (c *Context) ShouldBindUri(obj any) error {
|
||||||
m := make(map[string][]string, len(c.Params))
|
m := make(map[string][]string, len(c.Params))
|
||||||
for _, v := range c.Params {
|
for _, v := range c.Params {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user