1

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

3
  • 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? Commented Jul 6, 2018 at 8: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 Commented Jul 6, 2018 at 9:14
  • Without a minimal, complete, and verifiable example it is IMHO impossible to answer this question. Commented Jul 7, 2018 at 22:26

2 Answers 2

2

use Json decode: req is *http.Request

decoder := json.NewDecoder(req.Body)
decoder.UseNumber()
err := decoder.Decode(&yourStruct)
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Then I suspect you do not mean "query parameters" and you actually have JSON data inside the request body

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.