Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure ring middleware to handle url array

Tags:

clojure

ring

The ClojureScript cljs-ajax client library converts {:b [1 2]} to b[0]=1&b[1]=2

For example:

(http/get "http://example.com" {:b [1 2]})

results in a request to:

"http://example.com?b[0]=1&b[1]=2"

How can I setup my ring middleware to handle this format on the server side? I would like to convert it back to the original structure:

{:b [1 2]}

I am using the middleware below, but it does not work properly:

(ring.middleware.keyword-params/wrap-keyword-params)
(ring.middleware.params/wrap-params :encoding encoding)
(ring.middleware.nested-params/wrap-nested-params)
like image 341
Mamun Avatar asked Dec 13 '16 15:12

Mamun


1 Answers

There is no issue in middleware side. The issue is in cljs-ajax's ajax.core/params-to-str api. It is generating duplicate URL for different data format.

(ajax.core/params-to-str {:b [1 3]})
;; => "b[0]=1&b[1]=3"

(ajax.core/params-to-str {:b {0 1 1 3}})
;; => "b[0]=1&b[1]=3"

For array, format should be b[]=1&b[]=3.

like image 99
Mamun Avatar answered Oct 05 '22 08:10

Mamun