Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial import

  • Loading branch information...
commit bf833fbbe8c34dc8cb472d496ee704875f876b65 0 parents
@mmcgrana authored
4 .gitignore
@@ -0,0 +1,4 @@
+lib
+classes
+*.jar
+pom.xml
3  README.md
@@ -0,0 +1,3 @@
+# `ring-json-params`
+
+Ring middleware that augments `:params` according to a parsed JSON request body.
6 project.clj
@@ -0,0 +1,6 @@
+(defproject ring-json-params "0.1.0-SNAPSHOT"
+ :description "Ring middleware for JSON params parsing."
+ :dependencies
+ [[org.clojure/clojure "1.2.0-beta1"]
+ [org.clojure/clojure-contrib "1.2.0-beta1"]
+ [clj-json "0.3.0-SNAPSHOT"]])
19 src/ring/middleware/json_params.clj
@@ -0,0 +1,19 @@
+(ns ring.middleware.json-params
+ (:require [clj-json.core :as json])
+ (:require [clojure.contrib.io :as io]))
+
+(defn- json-request?
+ [req]
+ (if-let [#^String type (:content-type req)]
+ (.startsWith type "application/json")))
+
+(defn wrap-json-params [handler]
+ (fn [req]
+ (if-let [body (and (json-request? req) (:body req))]
+ (let [bstr (io/slurp* body)
+ json-params (json/parse-string bstr)
+ req* (assoc req
+ :json-params json-params
+ :params (merge (:params req) json-params))]
+ (handler req*))
+ (handler req))))
Please sign in to comment.
Something went wrong with that request. Please try again.