300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > golang接收 post和get请求参数处理

golang接收 post和get请求参数处理

时间:2022-09-09 03:20:18

相关推荐

golang接收 post和get请求参数处理

golang接收 post和get请求

1、golang中获取请求接口中数据(GET)

方式一:API参数ctx.Param(name string)或者ctx.Params.ByName(name string)

前端请求为:

"http://localhost:8080/api/book/paging/"+this.pageNum+"/"+this.pageSize//形式为:"http://localhost:8080/api/book/paging/2/2

此时后端路由写为:

r.GET("/api/book/paging/:page_num/:page_size",controller.Paging)

后端接收路径中参数:

pageSize,_:=strconv.Atoi(ctx.Param("page_size"))//它是下面的简写pageNum,_:=strconv.Atoi(ctx.Params.ByName("page_num"))

方式二:URL参数ctx.Query(name string)

前端请求为:

"http://localhost:8080/api/book/paging?page_num="+this.pageNum+"&page_size="+this.pageSize//形式为:"http://localhost:8080/api/book/paging?page_num=2&page_size=2

此时后端路由写为:

r.GET("/api/book/paging",controller.Paging)

后端接收路径中参数:

pageSize,_:=strconv.Atoi(ctx.Query("page_size"))pageNum,_:=strconv.Atoi(ctx.Query("page_num"))

2、golang中获取请求接口中数据(POST)

方式1:

var requestUser=model.User{}_=ctx.Bind(&requestUser)//获取参数telephone:=requestUser.Telephonepassword:=requestUser.Password

方式2:

//使用map获取请求的参数var requestMap=make(map[string]string)_ = json.NewDecoder(ctx.Request.Body).Decode(&requestMap)

方式3:

var requestRegister=model.User{}json.NewDecoder(ctx.Request.Body).Decode(&requestRegister)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。