Get请求参数
普通参数
请求
http://localhost:8080/query?id=123&name=user
接收
第一种方式
r.GET("/query", func(context *gin.Context) {
// 获取传参
id := context.Query("id")
name := context.Query("name")
// 设置某个参数的默认值
sex := context.DefaultQuery("sex", "unknown")
// 检查address值是否传入
address, ok := context.GetQuery("address")
context.JSON(http.StatusOK, gin.H{
"id": id,
"name": name,
"sex": sex,
"address": address,
"address-ok": ok,
})
})
第二种方式
提前定义结构体
// 定义结构体需要表示form名称,如果不标识则传参时key必须和结构体的对应的值一样比如Id就必须传入Id,id是不行的
type User struct {
Id int64 `form:"id"'`
Name string `form:"name"`
Sex string `form:"sex"`
// binding是指传参必须拥有此参数
Address string `form:"address" binding:"required"'`
}
r.GET("/query", func(context *gin.Context) {
var user User
err := context.BindQuery(&user)
if err != nil {
log.Println(err)
}
context.JSON(http.StatusOK, gin.H{
"id": user.Id,
"name": user.Name,
"sex": user.Sex,
"address": user.Address,
})
})
第三种方式
提前定义结构体
// 定义结构体需要表示form名称,如果不标识则传参时key必须和结构体的对应的值一样比如Id就必须传入Id,id是不行的
type User struct {
Id int64 `form:"id"'`
Name string `form:"name"`
Sex string `form:"sex"`
// binding是指传参必须拥有此参数
Address string `form:"address" binding:"required"'`
}
r.GET("/query", func(context *gin.Context) {
var user User
err := context.ShouldBindQuery(&user)
if err != nil {
log.Println(err)
}
context.JSON(http.StatusOK, gin.H{
"id": user.Id,
"name": user.Name,
"sex": user.Sex,
"address": user.Address,
})
})
返回值
第一种方式
状态码200
{
"address": "",
"address-ok": false,
"id": "123",
"name": "user",
"sex": "unknown"
}
第二种方式
状态码400
后台会报:
2023/01/27 22:18:55 Key: 'User.Address' Error:Field validation for 'Address' failed on the 'required' tag
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 200
{
"address": "",
"id": 123,
"name": "user",
"sex": ""
}
第三种方式
状态码200
后台会报:
2023/01/27 22:19:33 Key: 'User.Address' Error:Field validation for 'Address' failed on the 'required' tag
{
"address": "",
"id": 123,
"name": "user",
"sex": ""
}
数组参数
请求
http://localhost:8080/query?address=beijing&address=qingdao
接收
第一种方式
r.GET("/query", func(context *gin.Context) {
address := context.QueryArray("address")
context.JSON(http.StatusOK, address)
})
第二种方式
提前定义结构体
type Address struct {
Address []string `form:"address"`
}
r.GET("/query", func(context *gin.Context) {
var address Address
err := context.BindQuery(&address)
if err != nil {
log.Println(err)
}
context.JSON(http.StatusOK, address)
})
第三种方式
提前定义结构体
type Address struct {
Address []string `form:"address"`
}
r.GET("/query", func(context *gin.Context) {
var address Address
err := context.ShouldBindQuery(&address)
if err != nil {
log.Println(err)
}
context.JSON(http.StatusOK, address)
})
返回值
第一种方式
状态码200
[
"beijing",
"qingdao"
]
第二种方式
状态码200
{
"Address": [
"beijing",
"qingdao"
]
}
第三种方式
状态码200
{
"Address": [
"beijing",
"qingdao"
]
}
map参数
请求
http://localhost:8080/query?addressMap[home]=beijing&addressMap[company]=qingdao
接收
r.GET("/query", func(context *gin.Context) {
addressMap := context.QueryMap("addressMap")
context.JSON(http.StatusOK, addressMap)
})
返回值
状态码200
{
"company": "qingdao",
"home": "beijing"
}
POST请求
post请求一般是表单参数和json参数
表单请求
请求
form-data
id=123
name=user
address=beijing
address=qingdao
addressMap[home]=beijing
addressMap[company]=qingdao
接收
r.POST("/query", func(context *gin.Context) {
id := context.PostForm("id")
name := context.PostForm("name")
address := context.PostFormArray("address")
addressMap := context.PostFormMap("addressMap")
context.JSON(http.StatusOK, gin.H{
"id": id,
"name": name,
"address": address,
"addressMap": addressMap,
})
})
PS:
接收也可以使用func (c *gin.Context) ShouldBind(obj any) error {}函数进行接收和上面get讲的差不多一样这里不再讲解
返回值
状态码200
{
"address": [
"beijing",
"qingdao"
],
"addressMap": {
"company": "qingdao",
"home": "beijing"
},
"id": "123",
"name": "user"
}
JSON请求
请求
raw or json
{
"address": [
"beijing",
"qingdao"
],
"addressMap": {
"company": "qingdao",
"home": "beijing"
},
"id": 123,
"name": "user",
"sex":"Male"
}
接收
定义结构体,接收json数据结构体不需要加form表示,接收时会自动把Name转换为name进行匹配接收或者添加json的标识进行匹配
type User struct {
Id int64 `json:"id"`
Name string
Sex string
Address []string
AddressMap map[string]string
}
r.POST("/query", func(context *gin.Context) {
var user User
err := context.ShouldBindJSON(&user)
if err != nil {
log.Println(err)
}
context.JSON(http.StatusOK, user)
})
返回值
状态码200
{
"Id": 123,
"Name": "user",
"Sex": "Male",
"Address": [
"beijing",
"qingdao"
],
"AddressMap": {
"company": "qingdao",
"home": "beijing"
}
}
文件请求
代码实现
r.POST("/save", func(context *gin.Context) {
form, err := context.MultipartForm()
if err != nil {
log.Println(err)
}
for _, fileArray := range form.File {
for _, v := range fileArray {
context.SaveUploadedFile(v, "./"+v.Filename)
}
}
context.JSON(http.StatusOK, form.Value)
})
context.MultipartForm()返回的from里面有俩值,一个是文件数组,还有一个是提交的参数。上传好的文件会被context.SaveUploadedFile保存到本地