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