0

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)
}
18
  • 1
    You can call the Query method on the http.Request.URL field, this will return a value on which you can then call the Get method to get the query values by name. i.e. r.URL.Query().Get("param2"). Commented Apr 28, 2020 at 11:16
  • 1
    I don't exactly understand what you mean, 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. Commented Apr 28, 2020 at 12:12
  • 1
    I still don't understand, you've already provided a url with multiple parameters in your question and your own comment. Show me what you've tried, update the question with the code you've written, the input that your code is supposed to handle, and the output that you expect but were unable to produce. Commented Apr 28, 2020 at 12:22
  • 1
    Your project currently doesn't register any handler for the /login endpoint 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 use r.URL.Query().Get("id") to get [email protected] and r.URL.Query().Get("pwd") to get dccccf. If you want to avoid the unnecessary re-parsing of the query you can store the intermediate result of r.URL.Query() into a variable and execute the two Get calls on that. Commented Apr 28, 2020 at 12:52
  • 1
    Large code snippets like this are very hard to read in comments. Please update your question by including this piece of code. It seems the most relevant to your question. Commented Apr 28, 2020 at 15:11

1 Answer 1

3

Add the following in main.go

r.GET("/login",uc.LoginUser)

and add the following in controllers/user.go

func (uc UserController) LoginUser(w http.ResponseWriter, request *http.Request, params httprouter.Params) {
    emailId := request.URL.Query().Get("id")
    pwd := request.URL.Query().Get("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)

}
Sign up to request clarification or add additional context in comments.

1 Comment

Not really related to the question, I suggest to use form data method when submitting password info instead of query params. Then we can use r.FormValue("id") and r.FormValue("pwd")

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.