-
-
Save jcromartie/8e68edf100684ddcadfd to your computer and use it in GitHub Desktop.
This file contains 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
(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