gin框架中强大的context,方便人们取参,绑定参数
绑定参数
// 绑定 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")
}
取参数
// Param 返回URL上的参数,如/:id
// 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
// Query 如果查询参数存在,返回,否则返回空字符串""
// It is shortcut for `c.Request.URL.Query().Get(key)`
// GET /path?id=1234&name=Manu&value=
// c.Query("id") == "1234"
// c.Query("name") == "Manu"
// c.Query("value") == ""
// c.Query("wtf") == ""
func (c *Context) Query(key string) string
// DefaultQuery 如果查询参数存在,返回参数,否则返回指定的默认值
// See: Query() and GetQuery() for further information.
// GET /?name=Manu&lastname=
// c.DefaultQuery("name", "unknown") == "Manu"
// c.DefaultQuery("id", "none") == "none"
// c.DefaultQuery("lastname", "none") == ""
func (c *Context) DefaultQuery(key, defaultValue string) string
// GetQuery is like Query(),
// 若存在该参数,bool为true,否则为false,若存在但没有值,返回""
// It is shortcut for `c.Request.URL.Query().Get(key)`
// GET /?name=Manu&lastname=
// ("Manu", true) == c.GetQuery("name")
// ("", false) == c.GetQuery("id")
// ("", true) == c.GetQuery("lastname")
func (c *Context) GetQuery(key string) (string, bool)
// QueryArray 返回字符串数组
// The length of the slice depends on the number of params with the given key.
func (c *Context) QueryArray(key string) []string
// GetQueryArray 返回字符串数组,bool表示是否存在
func (c *Context) GetQueryArray(key string) ([]string, bool)
// QueryMap 返回map
func (c *Context) QueryMap(key string) map[string]string
// GetQueryMap 返回map,bool表示是否存在
func (c *Context) GetQueryMap(key string) (map[string]string, bool)
// PostForm 从 POST urlencoded form 或 multipart form 返回值,不存在返回空字符串
func (c *Context) PostForm(key string) string
// DefaultPostForm 多了默认值的设定
// See: PostForm() and GetPostForm() for further information.
func (c *Context) DefaultPostForm(key, defaultValue string) string
// GetPostForm 多了bool用来判断有没有这个参数
// For example, during a PATCH request to update the user's email:
// email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"
// email= --> ("", true) := GetPostForm("email") // set email to ""
// --> ("", false) := GetPostForm("email") // do nothing with email
func (c *Context) GetPostForm(key string) (string, bool)
// PostFormArray 字符串数组
// The length of the slice depends on the number of params with the given key.
func (c *Context) PostFormArray(key string) []string
// GetPostFormArray 多了是否存在
// a boolean value whether at least one value exists for the given key.
func (c *Context) GetPostFormArray(key string) ([]string, bool)
// PostFormMap 返回map
func (c *Context) PostFormMap(key string) map[string]string
// GetPostFormMap 多返回一个是否存在
func (c *Context) GetPostFormMap(key string) (map[string]string, bool)
// FormFile 返回第一个文件
func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
// MultipartForm is the parsed multipart form, including file uploads.
func (c *Context) MultipartForm() (*multipart.Form, error)
// SaveUploadedFile 直接把文件保存到一个位置
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error