Skip to content

Instantly share code, notes, and snippets.

@jjcomer
Created October 11, 2012 20:56
Show Gist options
  • Save jjcomer/3875427 to your computer and use it in GitHub Desktop.
Save jjcomer/3875427 to your computer and use it in GitHub Desktop.
JSON Middleware
(defn- json-request?
[req]
(if-let [#^String type (:content-type req)]
(not (empty? (re-find #"^application/(vnd.+)?json" type)))))
(defn wrap-json-params [handler]
(fn [req]
(if-let [body (and (json-request? req) (:body req))]
(let [bstr (slurp body)
json-params (parse-string bstr)
req* (assoc req
:json-params json-params
:params (merge (:params req) json-params))]
(handler req*))
(handler req))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment