mirror of
https://github.com/gin-gonic/gin.git
synced 2025-08-06 15:50:30 +08:00
Improves documentation
This commit is contained in:
23
context.go
23
context.go
@ -185,7 +185,8 @@ func (c *Context) MustGet(key string) interface{} {
|
||||
|
||||
// Query is a shortcut for c.Request.URL.Query().Get(key)
|
||||
// It is used to return the url query values.
|
||||
// ?id=1234&name=Manu
|
||||
// It returns an empty string ("") when the value does not exist.
|
||||
// /path?id=1234&name=Manu
|
||||
// c.Query("id") == "1234"
|
||||
// c.Query("name") == "Manu"
|
||||
// c.Query("wtf") == ""
|
||||
@ -195,12 +196,18 @@ func (c *Context) Query(key string) string {
|
||||
}
|
||||
|
||||
// PostForm is a shortcut for c.Request.PostFormValue(key)
|
||||
// It returns an empty string ("") when the value does not exist.
|
||||
func (c *Context) PostForm(key string) string {
|
||||
value, _ := c.postForm(key)
|
||||
return value
|
||||
}
|
||||
|
||||
// Param is a shortcut for c.Params.ByName(key)
|
||||
// Param returns the value of the URL param.
|
||||
// It is a shortcut for c.Params.ByName(key)
|
||||
// router.GET("/user/:id", func(c *gin.Context) {
|
||||
// // a GET request to /user/john
|
||||
// id := c.Param("id") // id == "john"
|
||||
// })
|
||||
func (c *Context) Param(key string) string {
|
||||
return c.Params.ByName(key)
|
||||
}
|
||||
@ -214,11 +221,9 @@ func (c *Context) DefaultPostForm(key, defaultValue string) string {
|
||||
|
||||
// DefaultQuery returns the keyed url query value if it exists, othewise it returns the
|
||||
// specified defaultValue.
|
||||
// ```
|
||||
// /?name=Manu
|
||||
// c.DefaultQuery("name", "unknown") == "Manu"
|
||||
// c.DefaultQuery("id", "none") == "none"
|
||||
// ```
|
||||
// //?name=Manu
|
||||
// c.DefaultQuery("name", "unknown") == "Manu"
|
||||
// c.DefaultQuery("id", "none") == "none"
|
||||
func (c *Context) DefaultQuery(key, defaultValue string) string {
|
||||
if value, ok := c.query(key); ok {
|
||||
return value
|
||||
@ -250,8 +255,8 @@ func (c *Context) postForm(key string) (string, bool) {
|
||||
|
||||
// Bind checks the Content-Type to select a binding engine automatically,
|
||||
// Depending the "Content-Type" header different bindings are used:
|
||||
// "application/json" --> JSON binding
|
||||
// "application/xml" --> XML binding
|
||||
// "application/json" --> JSON binding
|
||||
// "application/xml" --> XML binding
|
||||
// otherwise --> returns an error
|
||||
// If Parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
|
||||
// It decodes the json payload into the struct specified as a pointer.
|
||||
|
Reference in New Issue
Block a user