记录一下获取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
| $ curl localhost:9090/updateUser/hanpy/20 {"age":"20","birth":"","name":"hanpy"}
|
接收单个值
主要用到下面几个函数Query
、DefaultQuery
、GetQuery
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) { name := ctx.Query("name") age := ctx.DefaultQuery("age", "30") 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
| $ curl localhost:9090/url/get\?name=hanpy\&age=2 {"age":"2","birth":"","birthIsExist":false,"name":"hanpy"}%
$ curl localhost:9090/url/get\?name=hanpy\&age=2\&birth=1991 {"age":"2","birth":"1991","birthIsExist":true,"name":"hanpy"}%
|
接收数组
主要使用QueryArray
和 GetQueryArray
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
主要使用 QueryMap
和 GetQueryMap
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
| $ curl http://127.0.0.1:9090/map/get?user[lisi]=20&user[wangwu]=30 { "lisi": "20", "wangwu": "30" }
$ curl http://127.0.0.1:9090/map/get1?user[lisi]=20&user[wangwu]=30 { "isExist": true, "user": { "lisi": "20", "wangwu": "30" } }
|
POST参数获取
接收单个值
主要使用 PostForm
、DefaultPostForm
和 GetPostForm
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) { name := ctx.PostForm("name") age := ctx.DefaultPostForm("age", "30") 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
| $ curl -d "name=hanpy&age=30&birth=1991" http://127.0.0.1:9090/post/user { "age": "30", "birth": "1991", "birthIsExist": true, "name": "hanpy" }
|
接收数组
主要使用 PostFormArray
和 GetPostFormArray
1 2 3 4 5 6 7 8 9 10 11
| postRoute.POST("/arr", func(ctx *gin.Context) { list := ctx.PostFormArray("list[]") 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
| $ curl -d "list[]=red&list[]=green&like[]=sleep" http://127.0.0.1:9090/post/arr { "isExist": true, "like": [ "sleep" ], "list": [ "red", "green" ] }
|
接收Map
主要使用 PostFormMap
和 GetPostFormMap
1 2 3 4 5 6 7 8 9 10 11
| postRoute.POST("/map", func(ctx *gin.Context) { userMap := ctx.PostFormMap("user") 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
| $ 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" } }
|