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

时间:2018-07-06 07:27:55

标签: go postdata httprouter

我刚接触golang,并尝试使用httprouter(https://github.com/julienschmidt/httprouter)通过POST方法创建REST API。 我使用标头为Content-Type的简单原始请求:application / json。

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

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

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

2 个答案:

答案 0 :(得分:1)

使用Json解码: 要求是* http.Request

decoder := json.NewDecoder(req.Body)
decoder.UseNumber()
err := decoder.Decode(&yourStruct)

答案 1 :(得分:0)

您需要从URL中获取查询参数。

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

docs here