原文链接:https://blog.csdn.net/keyunq/article/details/82226280
一直都是用结构体接收参数,假如事先不清楚参数名,或者参数是不固定的,就要动态获取。
ctx *gin.Context
form方式的请求:
ctx.Request.ParseForm()
for k, v := range ctx.Request.PostForm {
fmt.Printf("k:%v
", k)
fmt.Printf("v:%v
", v)
}
json方式的请求:
data, _ := ioutil.ReadAll(ctx.Request.Body)
fmt.Printf("ctx.Request.body: %v", string(data))
1
2
然后从data里解析出来
代码段记录
logging.Debugf("c.Request.Method: %v", ctx.Request.Method)
logging.Debugf("c.Request.ContentType: %v", ctx.ContentType())
logging.Debugf("c.Request.Body: %v", ctx.Request.Body)
ctx.Request.ParseForm()
logging.Debugf("c.Request.Form: %v", ctx.Request.PostForm)
for k, v := range ctx.Request.PostForm {
logging.Debugf("k:%v
", k)
logging.Debugf("v:%v
", v)
}
logging.Debugf("c.Request.ContentLength: %v", ctx.Request.ContentLength)
data, _ := ioutil.ReadAll(ctx.Request.Body)
logging.Debugf("c.Request.GetBody: %v", string(data))
//第二种:
package main
import (
"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)*/