gorilla / mux

Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
20.89k stars 1.85k forks source link

[FEATURE] Support multi-value query string parameters #754

Open silverskater opened 7 months ago

silverskater commented 7 months ago

Is there an existing feature request for this?

Is your feature request related to a problem? Please describe.

When using a multi-value query string parameter only the first value is checked.

Url ENDPOINT?sections=90&sections=130 with this check:

r.Queries("sections", "130")

It doesn't match, and it should because the only key-value required matches, but as it only checks the first it encounters (sections=90 in this case), it determines it doesn't match. getURLQuery() only looks for the first query key: findFirstQueryKey().

Describe the solution that you would like.

Multi-value query params like sections=90&sections=130 should be matched by both commands from below, regardless of the order in the query string:

r.Queries("sections", "130")
r.Queries("sections", "90", "sections", "130")

Describe alternatives you have considered.

No response

Anything else?

This issue came up here: https://github.com/friendsofgo/killgrave/issues/164

Ranveer777 commented 5 months ago

@silverskater For multi-value query params use r.Queries("sections", "{sections}"). It will allow you to pass any and n number of sections in query.

You can also refer https://github.com/gorilla/mux/blob/de7178dc9dffadc3cf56bece3962737e8b0710b8/route.go#L400 for the usecase of Queries function.

Also, to restrict the value of sections in query params to 130 and 90 use r.Queries("sections", "{sections:(?:90|130)}")

silverskater commented 5 months ago

thank you @Ranveer777 this is exactly what we need!

joanlopez commented 5 months ago

Hey,

I don't think that's the same logic as having support for multi-value queries, @Ranveer777, I think what you're suggesting is just doing the trick with a pattern (regexp).

For instance, how would you define an endpoint that explicitly requires "both" values, not just "any"?

Thanks!