Gin框架学习记录 - 接收参数
hanpy

记录一下获取GET和POST参数的方式和注意事项。

GET参数获取

路由参数获取

路由格式为:/path/:a/:b 时,:x指的就是路由参数,可以直接通过Param("x")获取值信息

1
2
3
4
5
6
7
8
9
10
11
func RouteParam() {
r := gin.Default()
r.GET("/updateUser/:name/:age", func(ctx *gin.Context) {
name := ctx.Param("name")
age := ctx.Param("age")
// 找不到就会返回空字符串
birth := ctx.Param("birth")
ctx.JSON(200, gin.H{"name": name, "age": age, "birth": birth})
})
_ = r.Run(":9090")
}

访问一下

1
2
3
#1
$ curl localhost:9090/updateUser/hanpy/20
{"age":"20","birth":"","name":"hanpy"}

接收单个值

主要用到下面几个函数QueryDefaultQueryGetQuery
Query: 不存在就返回空字符串
DefaultQuery: 不存在就返回后面默认的值
GetQuery: 显式的返回是否存在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func RouteUrl(r *gin.Engine) {
urlRoute := r.Group("/url")
{
urlRoute.GET("/get", func(ctx *gin.Context) {
// #1 不存在就返回空字符串
name := ctx.Query("name")
// #2 不存在就返回后面默认的值
age := ctx.DefaultQuery("age", "30")
// #3 显式的返回是否存在
birth, isExist := ctx.GetQuery("birth")
ctx.JSON(200, gin.H{
"name": name,
"age": age,
"birth": birth,
"birthIsExist": isExist,
})
})
}
}
1
2
3
4
5
6
7
#1 
$ curl localhost:9090/url/get\?name=hanpy\&age=2
{"age":"2","birth":"","birthIsExist":false,"name":"hanpy"}%

#2
$ curl localhost:9090/url/get\?name=hanpy\&age=2\&birth=1991
{"age":"2","birth":"1991","birthIsExist":true,"name":"hanpy"}%

接收数组

主要使用QueryArrayGetQueryArray

1
2
3
4
5
6
7
8
9
10
11
12
arrRoute := r.Group("/array")
{
arrRoute.GET("/get", func(ctx *gin.Context) {
name := ctx.QueryArray("name[]")
age, isExist := ctx.GetQueryArray("age[]")
ctx.JSON(200, gin.H{
"name": name,
"age": age,
"ageIsExist": isExist,
})
})
}
1
2
3
4
5
6
7
8
9
10
11
12
$ curl http://127.0.0.1:9090/array/get?name[]=zhangsan&name[]=lisi&age[]=20&age[]=30
{
"age": [
"20",
"30"
],
"ageIsExist": true,
"name": [
"zhangsan",
"lisi"
]
}

接收Map

主要使用 QueryMapGetQueryMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mapRoute := r.Group("/map")
{
mapRoute.GET("/get", func(ctx *gin.Context) {
user := ctx.QueryMap("user")
ctx.JSON(200, user)
})
mapRoute.GET("/get1", func(ctx *gin.Context) {
user, isExist := ctx.GetQueryMap("user")
ctx.JSON(200, gin.H{
"user": user,
"isExist": isExist,
})
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#1 
$ curl http://127.0.0.1:9090/map/get?user[lisi]=20&user[wangwu]=30
{
"lisi": "20",
"wangwu": "30"
}

#2
$ curl http://127.0.0.1:9090/map/get1?user[lisi]=20&user[wangwu]=30
{
"isExist": true,
"user": {
"lisi": "20",
"wangwu": "30"
}
}

POST参数获取

接收单个值

主要使用 PostFormDefaultPostFormGetPostForm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
postRoute := r.Group("/post")
{
postRoute.POST("/user", func(ctx *gin.Context) {
// #1 不存在就返回空字符串
name := ctx.PostForm("name")
// #2 不存在就返回后面默认的值
age := ctx.DefaultPostForm("age", "30")
// #3 显式的返回是否存在
birth, isExist := ctx.GetPostForm("birth")
ctx.JSON(200, gin.H{
"name": name,
"age": age,
"birth": birth,
"birthIsExist": isExist,
})
})
}
1
2
3
4
5
6
7
8
# 1 
$ curl -d "name=hanpy&age=30&birth=1991" http://127.0.0.1:9090/post/user
{
"age": "30",
"birth": "1991",
"birthIsExist": true,
"name": "hanpy"
}

接收数组

主要使用 PostFormArrayGetPostFormArray

1
2
3
4
5
6
7
8
9
10
11
postRoute.POST("/arr", func(ctx *gin.Context) {
// #1 如果不存在返回空字符串切片
list := ctx.PostFormArray("list[]")
// #2 显式的返回是否有
like, isExist := ctx.GetPostFormArray("like[]")
ctx.JSON(200, gin.H{
"list": list,
"like": like,
"isExist": isExist,
})
})
1
2
3
4
5
6
7
8
9
10
11
12
#1
$ curl -d "list[]=red&list[]=green&like[]=sleep" http://127.0.0.1:9090/post/arr
{
"isExist": true,
"like": [
"sleep"
],
"list": [
"red",
"green"
]
}

接收Map

主要使用 PostFormMapGetPostFormMap

1
2
3
4
5
6
7
8
9
10
11
postRoute.POST("/map", func(ctx *gin.Context) {
// #1 如果不存在就返回空的 map[string]string
userMap := ctx.PostFormMap("user")
// #2 显式的返回是否存在
classMap, isExist := ctx.GetPostFormMap("class")
ctx.JSON(200, gin.H{
"userMap": userMap,
"classMap": classMap,
"isExist": isExist,
})
})
1
2
3
4
5
6
7
8
9
10
11
12
#1
$ curl -d "user[name]=hanpy&user[age]=20&class[one]=100" http://127.0.0.1:9090/post/map
{
"classMap": {
"one": "100"
},
"isExist": true,
"userMap": {
"age": "20",
"name": "hanpy"
}
}