dpp66953
2018-07-06 07:27
浏览 941
已采纳

在Golang中获取POST参数,并将标头作为application / json

I am new to golang and trying to create REST API with POST Method using httprouter (https://github.com/julienschmidt/httprouter). I am using simple raw request with header as Content-Type : application/json.

I have tried hard but not getting way to fetch raw query parameters.

req.FormValue("name") or req.Form.Get("name") is working fine but with header as Content-Type : application/x-www-form-urlencoded

Has anyone tried fetching raw query parameters(with header as Content-Type : application/json)?

图片转代码服务由CSDN问答提供 功能建议

我是golang的新手,正尝试使用Http方法使用POST方法创建REST API( https://github.com/julienschmidt/httprouter )。 我正在使用简单的原始请求,其标头为Content-Type:application /

我已经尽力了,但是没有办法获取原始查询参数。

req.FormValue(“ name”)或req.Form .Get(“ name”)工作正常,但标头为Content-Type:application / x-www-form-urlencoded

有人尝试获取原始查询参数(标头为Content) -Type:application / json)?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhoulei8959 2018-07-06 07:50
    已采纳

    use Json decode: req is *http.Request

    decoder := json.NewDecoder(req.Body)
    decoder.UseNumber()
    err := decoder.Decode(&yourStruct)
    
    点赞 打赏 评论
  • doulin1867 2018-07-06 07:34

    You need to grab the query params out of the URL.

    // req *http.Request
    params := req.URL.Query()
    myParam := params["my-query-param"]
    

    docs here

    点赞 打赏 评论

相关推荐 更多相似问题