Reputation: 4604
So I know that Gorilla Mux doesn't support optional query params and people suggest to create different routes with query param which makes it more solid. But in my case it is not resolving the routes as expected.
If I call /service/{locale}?param1=1,2¶m2=3,4
then it invokes a.listFirst
while it should call the a.listSecond
and the same issue with other combinations. But:
I know I can use request.URL.Query()
to get the query params but I am curious to know why this way of defining query params as route doesn't work as expected?
a.Router.
Methods(http.MethodGet).
Path("/service/{locale}").
Handler(negroni.New(
a.contentTypeJSON,
negroni.HandlerFunc(a.listAll),
))
a.Router.
Methods(http.MethodGet).
Path("/service/{locale}").
Queries("param1", "{param1:[0-9,]+}").
Handler(negroni.New(
a.contentTypeJSON,
negroni.HandlerFunc(a.listFirst),
))
a.Router.
Methods(http.MethodGet).
Path("/service/{locale}").
Queries("param2", "{param2:[0-9,]+}", "param1", "{param1:[0-9,]+}").
Handler(negroni.New(
a.contentTypeJSON,
negroni.HandlerFunc(a.listSecond),
))
I have already looked at these but they dont answer the question link1 link2
UPDATE
Short answer: Order matters
Tip: Don't be a fool and use correct handler function
Upvotes: 0
Views: 3791
Reputation: 88
I would suggest using grpc-go , grpc-gateway and play around with that, you need not define extra query params this way. everything that is defined in you message can be passed as query params
Upvotes: -1
Reputation: 1537
You have to change the order because the order matters. The first match gets selected
a.Router.
Methods(http.MethodGet).
Path("/service/{locale}").
Queries("param2", "{param2:[0-9,]+}", "param1", "{param1:[0-9,]+}").
Handler(negroni.New(
a.contentTypeJSON,
negroni.HandlerFunc(a.listSecond),
))
a.Router.
Methods(http.MethodGet).
Path("/service/{locale}").
Queries("param1", "{param1:[0-9,]+}").
Handler(negroni.New(
a.contentTypeJSON,
negroni.HandlerFunc(a.listFirst),
))
a.Router.
Methods(http.MethodGet).
Path("/service/{locale}").
Handler(negroni.New(
a.contentTypeJSON,
negroni.HandlerFunc(a.listAll),
))
Upvotes: 1