Search code examples
gopostdatahttprouter

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


Solution

  • use Json decode: req is *http.Request

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