From 14a462940d4ec82e39e1d5ff473cf59f828b8751 Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 5 Jan 2015 13:13:40 -0500 Subject: [PATCH] add auto-transform-body --- src/clj_webtoolbox/routes/checked.clj | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/clj_webtoolbox/routes/checked.clj b/src/clj_webtoolbox/routes/checked.clj index 808dc24..60dfcd1 100644 --- a/src/clj_webtoolbox/routes/checked.clj +++ b/src/clj_webtoolbox/routes/checked.clj @@ -176,3 +176,15 @@ [request] (let [body (body-string request)] (assoc request :body (edn/read-string body)))) + +(defn auto-transform-body + "Automatically transforms the body of the request, parsing it as JSON or EDN + based on the request's Content-Type header. The returned request will contain + a :body value with the parsed result, or the original value if the + Content-Type header was not recognized." + [request] + (let [body (body-string request)] + (condp = (:content-type request) + "application/json" (assoc request :body (json/parse-string body true)) + "application/edn" (assoc request :body (edn/read-string body)) + request))) \ No newline at end of file