When you create a funcHandler in Golang and use Gorilla Mux, I know that you can access a specific input variable by calling Mux.Vars. However, I'm not sure how that works when you have data stored in JSON format, and part of that is because I'm not sure how Mux.Vars() works. So, I'd like to know how to list all variables stored by Mux.Vars() when you enter a funcHandler and how to parse JSON stored in a URL (ie, /data?name="bill"&value="red", where I would want to find the value of the name and value keys).
Asked
Active
Viewed 2,479 times
-2
-
3Where in `/data?name="bill"&value="red"` should some JSON be hidden? What would be wrong with plain parsing the URL? – Volker Oct 19 '15 at 22:02
-
1`mux.Vars` is when you're using gorilla/mux's URL parameter binding. If you want to parse the query string params you can just use https://golang.org/pkg/net/http/#Request.FormValue or `request.URL` with `net/url` to parse them. – elithrar Oct 20 '15 at 00:06
1 Answers
0
For list all Gorilla Mux:
for k, v := range mux.Vars(request) {
log.Printf("key=%v, value=%v", k, v)
}
The Vars
function return a map
, for range
loop help you read all the items like I show you.
But I think your question is a bit different, if you want to read the JSON data or other kid of data that send with the request, you need to read the request Body (req.Body). Note that request Body is a Reader interface not a string. An example if you expect the input in JSON format:
Handling JSON Post Request in Go