Skip to content

Instantly share code, notes, and snippets.

@maxweber
Created March 1, 2011 10:05
Show Gist options
  • Save maxweber/848926 to your computer and use it in GitHub Desktop.
Save maxweber/848926 to your computer and use it in GitHub Desktop.
A clj-http middleware which automatically converts a JSON response into a corresponding Clojure data structure (from the clj-facebook-graph project)
(use 'clojure.contrib.json)
(defn wrap-json-response-conversion [client]
"Automatically transforms the body of a response of a Facebook Graph API request from JSON to a Clojure
data structure through the use of clojure.contrib.json. It checks if the header Content-Type
is 'text/javascript' which the Facebook Graph API returns in the case of a JSON response."
(fn [req]
(let [{:keys [headers] :as resp} (client req)
content-type (headers "content-type")]
(if (and (not (nil? content-type))
(.startsWith content-type "text/javascript"))
(assoc resp :body (read-json (:body resp)))
resp))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment