用postman模拟请求
get 参数
1、不带默认值
id := c.Query("id")
在这里我们获得了地址http://127.0.0.1:8089/post?id=1中的id参数
2、get参数默认值
id := c.DefaultQuery("id", "0")
如果查询不到,则给一个默认值
post 参数
用postman模拟请求

1、不带默认值
name := c.PostForm("name")
在这里我们获得了form表单中的post参数
2、带默认值
name := c.DefaultPostForm("name", "hanyun")
3、获取 []byte
data, _ := ioutil.ReadAll(ctx.Request.Body)
或者
package mainimport ("fmt""net/http""github.com/gin-gonic/gin")func main() {router := gin.Default()router.POST("/events", events)router.Run(":5000")}func events(c *gin.Context) {buf := make([]byte, 1024)n, _ := c.Request.Body.Read(buf)fmt.Println(string(buf[0:n]))resp := map[string]string{"hello": "world"}c.JSON(http.StatusOK, resp)/*post_gwid := c.PostForm("name")fmt.Println(post_gwid)*/}
数组参数
GetQueryArray
我们的请求地址http://127.0.0.1:8089/array?idList=1&idList=2&idList=3,用postman模拟测试

r.GET("/array", func(c *gin.Context) {if idList, err := c.GetQueryArray("idList"); err {fmt.Println(err)fmt.Println(idList[0])c.JSON(http.StatusOK, gin.H{"dataList": idList})} else {c.JSON(http.StatusOK, gin.H{"dataList": []string{}})}})
QueryArray接收数组参数
r.GET("/QueryArray", func(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"dataList": c.QueryArray("idList")})})
请求地址http://127.0.0.1:8089/QueryArray?idList=1&idList=2&idList=3我们得到的结果和GetQueryArray的数据一样
QueryMap接收参数
r.POST("/QueryMap", func(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"dataList": c.QueryMap("idList")})})
请求地址http://127.0.0.1:8089/QueryMap?idList[name]=hanyun&idList[password]=21212121,用postman模拟

PostFormMap接收参数
r.POST("/PostFormMap", func(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"dataList": c.PostFormMap("idList")})})
请求地址http://127.0.0.1:8089/PostFormMap,用postman模拟测试

9、PostFormArray接收参数
r.POST("/PostFormArray", func(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"dataList": c.PostFormArray("idList")})})
请求地址http://127.0.0.1:8089/PostFormArray,用postman模拟测试

Param获得参数
请求地址http://127.0.0.1:8089/param/hanyun,用postman模拟请求

