I want to get the map structure from the following Gorilla Mux router input.package main
For example,
router.Methods("GET").Path("/api/{action}").HandlerFunc(httpLog(myHandler))
func myHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Println(vars["action"])
}
Serves 0.0.0.0:3000/api/input
and this prints out the string input
What if I want to be able to receive requests like:
0.0.0.0:3000/api/v3?id=hello&password=great&product=ipad&confirm=true
And from this requests, I want to get a map of:
map["id"] = "hello"
map["password"] = "great"
map["product"] = "ipad"
map["confirm"] = "true"
Will you want me to do?
func myHandler(r http.ResponseWriter, q *http.Request) {
vars := mux.Vars(q)
fmt.Println(vars["action"])
fmt.Println(q.FormValue("id"))
fmt.Println(q.FormValue("password"))
fmt.Println(q.FormValue("product"))
fmt.Println(q.FormValue("confirm"))
}
You can use Queries method on you router
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter().Queries("id", "{id:[a-z]+}", "password", "{password:[a-z]+}", "product", "{product:[a-z]+}", "confirm", "{confirm:true|false}")
request, _ := http.NewRequest("GET", "http://example.com?id=hello&password=great&product=ipad&confirm=true", nil)
var match mux.RouteMatch
router.Match(request, &match)
fmt.Println(match.Vars)
}