I am new in Go. So please provide an example with your answer. I am using julienschmidt/httprouter. I am able to parse one parameter with this but how can I parse multiple parameters using this or any other library? The output I want to achieve is to get [email protected] & dccccf from this url:-> http://localhost:8080/[email protected]&pwd=dccccf
my code is at:- https://github.com/mohit810/prog-1
I tried r.GET("/login",uc.LoginUser) in main.go and
in controllers/user.go i added
func (uc UserController) LoginUser(w http.ResponseWriter, request *http.Request, params httprouter.Params) {
emailId := params.ByName("id")
pwd := params.ByName("pwd")
u := models.User{}
if err := uc.session.DB("go-web-dev-db").C("users").FindId(emailId + pwd).One(&u); err != nil {
w.WriteHeader(404)
return
}
uj, err := json.Marshal(u)
if err != nil {
fmt.Println(err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) // 200
fmt.Fprintf(w, "%s\n", uj)
}
Querymethod on the http.Request.URL field, this will return a value on which you can then call theGetmethod to get the query values by name. i.e.r.URL.Query().Get("param2").r.URL.Query()parses the url query parameter string (the whole part after the?in the url) and returns a value that is a key-to-list-of-values map on which you can then call Get with whatever param name you so desire, or you can loop over the map with a for-range loop. It can't get more dynamic then that./loginendpoint so you haven't really written anything yet to do what you want. Anyway to get the two values from such a query string you'd simply user.URL.Query().Get("id")to get[email protected]andr.URL.Query().Get("pwd")to getdccccf. If you want to avoid the unnecessary re-parsing of the query you can store the intermediate result ofr.URL.Query()into a variable and execute the twoGetcalls on that.