0

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

RKP
  • 750
  • 2
  • 12
  • 23
  • I'm confused. The title says you want POST parameters, but the question talks about JSON (which is totally different from forms). Then the question also mentions query parameters, which is has nothing to do with the content (i.e. body). So which is it? – Peter Jul 06 '18 at 08:58
  • Its like I have one API which accepts input parameters as POST. I want to get that input parameter or query parameters. And my API consumers will do post hit with a header as application/json. This is all about one single thing – RKP Jul 06 '18 at 09:14
  • Without a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) it is IMHO impossible to answer this question. – Daniel Schütte Jul 07 '18 at 22:26

2 Answers2

1

use Json decode: req is *http.Request

decoder := json.NewDecoder(req.Body)
decoder.UseNumber()
err := decoder.Decode(&yourStruct)
Hau Ma
  • 254
  • 2
  • 14
0

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

Zak
  • 5,515
  • 21
  • 33
  • 1
    Then I suspect you do not mean "query parameters" and you actually have JSON data inside the request body – Zak Jul 06 '18 at 09:37