Fetch POST Parameters in Golang with header as application/json

2k views Asked by At

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)?

2

There are 2 answers

0
Hau Ma On BEST ANSWER

use Json decode: req is *http.Request

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

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