Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Last active December 14, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/8e68edf100684ddcadfd to your computer and use it in GitHub Desktop.
Save jcromartie/8e68edf100684ddcadfd to your computer and use it in GitHub Desktop.
(defn json-request?
[request]
(when-let [type (:content-type request)]
(= type "application/json")))
(defn wrap-json-params
[handler]
(fn [request]
(if-let [body (and (json-request? request) (:body request))]
(let [[json msg] (try
[(json/read-str (slurp body) :key-fn keyword) nil]
(catch Exception e
[nil (.getMessage e)]))]
(if (not msg)
(-> request
(update-in [:json-params] merge json)
(update-in [:params] merge json)
handler)
{:status 400 :body (str "invalid JSON body: " msg)}))
(handler request))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment