Created
March 1, 2011 10:05
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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