r0man / cljs-http

A ClojureScript HTTP library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[client] - GET with duplicated query params

kernelp4nic opened this issue · comments

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

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.

@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"}})

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"]}})