开发者

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

开发者 https://www.devze.com 2023-03-09 11:06 出处:网络 作者: HSMgogogo!
目录1、golang中获取请求接口中数据(GET)方式一: API参数 ctx.Param(name string)或者ctx.Params.ByName(name string)方式二:URL参数 ctx.Query(name string)2、golang中获取请求接口中数据(POST)方式1:方式2:方
目录
  • 1、golang中获取请求接口中数据(GET)
    • 方式一: API参数 ctx.Param(name string)或者ctx.Params.ByName(name string)
    • 方式二:URL参数 ctx.Query(name string)
  • 2、golang中获取请求接口中数据(POST)
    • 方式1:
    • 方式2:
    • 方式3:

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.pagepythonNum+"&page_size="+this.pageSize
//形式为:"http://localhost:8080/api/book/paging?page_num=2&page_size=2

此时后端路由写为:

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

后端接收路径中参数:

pageSize,_http://www.devze.com:=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.Telephone
    password:=requestUser.Password

方式2:

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

方式3:

    var requestRewww.devze.comgister=model.User{}
   开发者_Go教程 json.NewDecoder(ctx.Request.Body).Decode(&requphpestRegister)

到此这篇关于golang接收post和get请求参数处理的文章就介绍到这了,更多相关golang post和get请求内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号