Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse query parameter /hello?name=username #9

Closed
abhijitkadam opened this issue Jun 12, 2014 · 7 comments
Closed

Parse query parameter /hello?name=username #9

abhijitkadam opened this issue Jun 12, 2014 · 7 comments
Labels

Comments

@abhijitkadam
Copy link

I am not sure email for help is mentioned or there is a mailing list for this project.

How do parse query parameter? Like I have /hello?name=abhijit the for this pattern I should get "abhijit" when I read from ps.ByName("name"), however unable to read

@julienschmidt
Copy link
Owner

The router matches against the request method and the requested URL path only. ?name=abhijit is the URL query and not part of the URL path.

You can get the query parameter values e.g. this way:

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    queryValues := r.URL.Query()
    fmt.Fprintf(w, "hello, %s!\n", queryValues.Get("name"))
}

@b35li
Copy link

b35li commented Apr 9, 2015

@julienschmidt
How to write the router? the followed code does NOT work??
<code
router.GET("/hello?name=xxx", Hello)

@julienschmidt
Copy link
Owner

router.GET("/hello", Hello), then use

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    queryValues := r.URL.Query()
    fmt.Fprintf(w, "hello, %s!\n", queryValues.Get("name"))
}

The URL query is NOT part of the URL path. The router only matches against the request method and the path.

@b35li
Copy link

b35li commented Apr 9, 2015

Thanks for quick response. Gratitude.

But after looking through the README and testing, the router should go like this

router.GET("/hello/*filepath", Hello)

then to parse the URL. It works.

@preetamyadav
Copy link

http://localhost:8080/api/v1/itineraries/all?auth-token=KeQEtE8VI3duSqXHnrDoFKMeilRfmE&auth-email=melissajwolff+testabc@gmail.com

when i tried to get

v := r.URL.Query()

it omittted the + sign from email .

@julienschmidt
Copy link
Owner

@preetamyadav Just must query-escape values first before using them in an URL query. + is used to escape spaces: https://play.golang.org/p/2o1jCP3Ut7

@preetamyadav
Copy link

Thanks @julienschmidt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants