Skip to content

Instantly share code, notes, and snippets.

@jjcomer
Created October 11, 2012 20:56

Revisions

  1. jjcomer created this gist Oct 11, 2012.
    15 changes: 15 additions & 0 deletions json-middleware.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    (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))))