简体   繁体   English

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

[英]Fetch POST Parameters in Golang with header as application/json

I am new to golang and trying to create REST API with POST Method using httprouter ( https://github.com/julienschmidt/httprouter ). 我刚接触golang,并尝试使用httprouter( https://github.com/julienschmidt/httprouter )使用POST方法创建REST API。 I am using simple raw request with header as Content-Type : application/json. 我正在使用简单的原始请求,其标头为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 req.FormValue(“ name”)或req.Form.Get(“ name”)工作正常,但标头为Content-Type:application / x-www-form-urlencoded

Has anyone tried fetching raw query parameters(with header as Content-Type : application/json)? 有没有人尝试获取原始查询参数(标头为Content-Type:application / json)?

use Json decode: req is *http.Request 使用Json解码:req是* http.Request

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

You need to grab the query params out of the URL. 您需要从URL中获取查询参数。

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

docs here 这里的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM