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 dgff@vcv.com & dccccf from this url:-> http://localhost:8080/login?id=dgff@vcv.com&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)
}
mkopriva
  • 35,176
  • 4
  • 57
  • 71
Mohit Yadav
  • 158
  • 3
  • 11
  • 1
    You can call the `Query` method on the [http.Request.URL](https://golang.org/pkg/net/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")`. – mkopriva Apr 28 '20 at 11:16
  • @mkopriva can u please provide me an example of any dynamic url like `http://localhost:8080/login?id=dwad@vcv.com&pwd=dccccf` – Mohit Yadav Apr 28 '20 at 12:05
  • 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. – mkopriva Apr 28 '20 at 12:12
  • @mkopriva can you provide any example that contains multiple parameters in url. that would be really helpful. Thanks in advance! – Mohit Yadav Apr 28 '20 at 12:16
  • 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. – mkopriva Apr 28 '20 at 12:22
  • @mkopriva I have updated the question with the desired output and source code – Mohit Yadav Apr 28 '20 at 12:37
  • 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 `dgff@vcv.com` 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. – mkopriva Apr 28 '20 at 12:52
  • @mkopriva see i know u r trying to help me but i don't know where to write `r.URL.Query().Get("id")` as i am a beginner? – Mohit Yadav Apr 28 '20 at 14:57
  • @mkopriva 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)}` – Mohit Yadav Apr 28 '20 at 15:06
  • 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. – mkopriva Apr 28 '20 at 15:11
  • @mkopriva i have updated the question. – Mohit Yadav Apr 28 '20 at 15:16
  • 1
    Now replace `params.ByName` with `request.URL.Query().Get`. – mkopriva Apr 28 '20 at 15:28
  • @mkopriva kuddos!!! I am really thankful to you!! – Mohit Yadav Apr 28 '20 at 15:37
  • @mkopriva i had one question. how and from where should i refer for programing in server side go lang? – Mohit Yadav Apr 28 '20 at 16:54
  • 1
    I can't recommend specific resources because I don't know of any, all I can recommend is that you use google a lot, there's a lot of articles on the internet about web application programming in Go, so just search and read, rinse and repeat. – mkopriva Apr 28 '20 at 16:59
  • @mkopriva hey every thing was working fine but when i used the backend code in production the android library is converting @ to %40. how to decode it in the backend as i used ` emailId, err := url.QueryUnescape(encodedValue)` with using it or without code on my device responses what can i do to get proper response in production server as i get 404 on android – Mohit Yadav May 06 '20 at 07:16
  • You should open a new question and be more specific when describing what the problem is. Right now it's unclear what the issue is because `.Query().Get()` in general results in an unescaped value and even if not you're already aware of `url.QueryUnescape` but you don't specify what is wrong with any of the code you have. Please open a new question and provide more details so that the problem is clear to everyone. – mkopriva May 06 '20 at 07:25
  • @mkopriva I have posted the question in detail on https://stackoverflow.com/questions/61652915/getting-404-in-get-request-in-go – Mohit Yadav May 07 '20 at 08:13

1 Answers1

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)

}
Logovskii Dmitrii
  • 2,629
  • 4
  • 27
  • 44
Mohit Yadav
  • 158
  • 3
  • 11
  • 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")` – Andre Suchitra Nov 09 '21 at 14:04