0

Below is a snapshot of a HTTP Get Call while using Gorilla Mux Router:

usersAPIs.HandleFunc("/users",
    middleware.WrapperHandler(th.List)).
    Queries("email", "{email}").
    Queries("order_by", "{order_by}").
    Queries("order_type", "{order_type}").
    Queries("page", "{page}").
    Queries("limit", "{limit}").
    Methods("GET")

Now when GET call happens with all query params e.g.

http://localhost:xxxx/accounts/users?email=a&page=1&limit=4&order_by=a&order_type=b 

then the gorilla mux router matches the pattern and takes it to the handler.

But when called like with fewer parameters e.g.

http://localhost:xxxx/accounts/users?email=a&page=1

then it says e.g. 404 not found means Resource path not mapped.

Questions :

#1. What has been missed here, is Go Gorilla Mux Router need all query params?

#2. What to be done If the GET query can come with zero or more parameters? e.g.

http://localhost:xxxx/accounts/users?email=a&page=1 

or

http://localhost:xxxx/accounts/users?page=1 
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Sumit Arora
  • 5,051
  • 7
  • 37
  • 57
  • 1
    You're misusing [`Queries`](https://godoc.org/github.com/gorilla/mux#Route.Queries), which is for adding matchers, it's not supposed to be used to just "pre-declare" query variables that you can then retrieve at your convenience. A matcher either matches or it doesn't. If doesn't the handler won't get invoked. What you should do is register a matcher only for the path and method and include only those query params that are *required* by the handler. – mkopriva Oct 09 '20 at 11:39
  • ... i.e. #1 Yes. #2 Remove every single `Queries` invocation from that code snippet. – mkopriva Oct 09 '20 at 11:53
  • Thanks @mkriva, I will look into it , – Sumit Arora Oct 09 '20 at 11:55
  • My question was one of the version of this : https://stackoverflow.com/questions/46045756/retrieve-optional-query-variables-with-gorilla-mux #1, Yes , Mux works that way, #2. In the http request handler : To Get Optional params from RequestURI, following worked for me : e.g. Email := r.URL.Query().Get("email") – Sumit Arora Oct 10 '20 at 05:55

1 Answers1

1

Queries(key, value) acts as a path matcher when passed to the route. Since 5 path params are expected, the route would match only if all 5 of them are present.

Though it's a little late to answer this question, but for anyone who stubles upon it later.

Kritika
  • 41
  • 2