I'm currently doing some REST API stuff in clojure, and I am using the ring.middleware.format library with compojure to transform JSON to and from clojure data structures.
I am having a huge issue, in that and JSON posted to the ring app will have all arrays replaced with the first item that was in the array. I.E. it will turn this JSON posted to it from
{
"buyer":"Test Name",
"items":[
{"qty":1,"size":"S","product":"Red T-Shirt"},
{"qty":1,"size":"M","product":"Green T-Shirt"}
],
"address":"123 Fake St",
"shipping":"express"
}
to this
{
"buyer": "Test Name",
"items": {
"qty": 1,
"size": "M",
"product": "Green T-Shirt"
},
"address": "123 Fake St",
"shipping": "express"
}
It does it for any arrays, including when an array is the root element.
I am using the following code in clojure to return the json:
(defroutes app-routes
(GET "/"
[]
{:body test-data})
(POST "/"
{data :params}
{:body data}))
;{:body (str "Printing " (count (data :jobs)) " jobs")}))
(def app
(-> (handler/api app-routes)
(wrap-json-params)
(wrap-json-response)))
The GET route has no issues with arrays and outputs properly, so it has to be either the way I am getting the data or the wrap-restful-params
middleware.
Any ideas?
I am having similar problem with ring-json-params. So I ended up using raw request body and parsing the JSON string myself.
I used the folliwng code:
(defroutes app-routes
(POST "/"
{params :body}
(slurp params)))
I use the clj-json.core library for parsing JSON.
Hope this helps. If you figured out a better way then please share. I am a Clojure/Compojure newbie!!!