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

[client] - GET with duplicated query params #47

Closed
kernelp4nic opened this issue Feb 15, 2015 · 3 comments
Closed

[client] - GET with duplicated query params #47

kernelp4nic opened this issue Feb 15, 2015 · 3 comments

Comments

@kernelp4nic
Copy link

Hello! I'm having a problem with duplicated query params, I need to receive a list of parameters on the server, I'm using compojure, so if I go with:

;; server
(defroutes routes
  (GET "/" {{:strs [list-of-vals]} :query-params :as request}
       (println list-of-vals)))
;; ["1" "2"]
;; client
GET /?list-of-vals=1&list-of-vals=2

This works great with an XMLHttpRequest (or other clients), but not with cljs-http.

So, if I try to parse multiple params with the same name:

(println (http/parse-query-params "/?list-of-vals=1&list-of-vals=2&list-of-vals=3"))
{:/api?list-of-vals 1, :list-of-vals 3}
(println (http/parse-query-params "/?list-of-vals=1&list-of-vals=2&list-of-values=2"))
{:/api?list-of-vals 1, :list-of-vals 2, :list-of-values 3}

Given that query params are handled with a map this behaviour is expected

(-> {}
    (assoc :list-of-vals 1)
    (assoc :list-of-vals 2))
;; {:list-of-vals 2}

Given all that, how do you handle a list of query params correctly? This looks like a common [1,2] practice.

1- http://stackoverflow.com/a/2602127/600052
2- http://stackoverflow.com/a/13261403/600052

@brentvatne
Copy link
Contributor

To add some more nuance to this issue, Rails uses [] after the parameter name to indicate that a given attribute is a list: ?issue_id[]=1&issue_id[]=2 - without that, the last value for a given param will be used, eg: ?issue_id=1&issue_id=2 will yield issue_id=2 on the server.

@ttasterisco
Copy link
Contributor

@kernelp4nic check this PR #17

the README also has the following example:

;; Form parameters in a POST request (array of values)
(http/post "http://example.com" {:form-params {:key1 [1 2 3] :key2 "value2"}})

@kernelp4nic
Copy link
Author

oh! thanks! I will send a PR with a GET example using query-params.
Something like:

(http/get "http://example.com" {:query-params {:foo ["foo" "bar"]}})

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

No branches or pull requests

3 participants