当前位置:网站首页>golang的gin框架,各种接收参数的方式和各种绑定的区别?

golang的gin框架,各种接收参数的方式和各种绑定的区别?

2022-06-12 02:59:00 给我一瓶冰阔洛

1.get请求

获取url中参数?id=1234&page=1

firstname := c.DefaultQuery("firstname", "Guest")  
lastname := c.Query("lastname") // 是 c.Request.URL.Query().Get("lastname") 

2.post 与get请求

r.GET("/user/:name/:action"           action := c.Param("action")

r.POST("/user/:name/:id"        action := c.Param("id")

 3.post表单提交             

types := c.DefaultPostForm("type", "post")
username := c.PostForm("username")
id,ok := c.GetPostForm("id")

4.json 请求


parms, _ := c.GetRawData()// 定义map或结构体
var req map[string]interface{}
_ = json.Unmarshal(parms, &req)// 反序列化
fmt.Println("req",req)
jsonReq, _ := json.Marshal(req) //map转string
fmt.Println("req",jsonReq)

原网站

版权声明
本文为[给我一瓶冰阔洛]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43035350/article/details/125180925