接口:
//POST请求 返回json 接口传参json r.POST("/postJson", controller.PostUserInfo)
1.定义结构体
- //定义结构体
- type Search struct {
- Id int
- Name string
- }
2.结构体传参
- //结构体传参
- func PostUserInfo(c *gin.Context) {
- search := &Search{}
- err := c.BindJSON(&search)
- if err == nil {
- ReturnSucess(c, 200, "请求成功",search, 1)
- return
- }
- ReturnError(c, 500, gin.H{"err": err}, search, 1)
- }
3.完整代码
- package controller
-
- import "github.com/gin-gonic/gin"
-
- //定义结构体
- type Search struct {
- Id int
- Name string
- }
-
-
- //结构体传参
- func PostUserInfo(c *gin.Context) {
- search := &Search{}
- err := c.BindJSON(&search)
- if err == nil {
- ReturnSucess(c, 200, "请求成功",search, 1)
- return
- }
- ReturnError(c, 500, gin.H{"err": err}, search, 1)
- }
4.接口测试及返回