Search code examples
clojureclojurescriptring

Using cljs-ajax to send an array as param in a GET call to a ring backend


I'm making a GET call to a ring backend using cljs-ajax.

The problem is that I need to pass an array too.

cljs-ajax encodes array like this:

?array[0]=one&array[1]=two

ring expects arrays to be encoded like this:

?array=one&array=two

So using the wrap-params middleware I don't get an array, but just different key-value pairs ({array[0] "one" array[1] "two}).

Is there a way to solve this, or do I have to manually parse the params on the server side?

Thank you


Solution

  • The limitation isn't with ring or cljs-ajax. It is a limitation in the http protocol. In a GET request, the params are just ?name=val&name=val, there is no mechanism for telling the server at the other end that the params represent an array of data - that is, distinguish between norm name/value pairs and name value pairs which should be interpreted as an array.

    Therefore, you need to manually convert the map generated by your ring middleware into an array yourself in your handler. You could use a heuristic to implement middleware which looks for get params with a specific 'pattern' in the param names, such as #"array[\d+\]" and have it extract them into an array and insert into your params map. However, I think this is a bit of a kludge and unless you need to do this in many of your handlers, it would likely introduce more issues than it solves.

    The easiest thing to do would be to convert your call to a post rather than a get and use json.