A Ring middleware that augments :params according to a parsed Clojure data literal request body.
- Source repository -- patches welcomed
- Official Announcement
In your :dependencies
section add the following:
[ring-clj-params "0.1.0"]
To use this middleware using Ring and Compojure, create a new Leiningen project with a project.clj
file of the form:
(defproject awesomeness "0.0.1"
:description ""true power awesomeness
:dependencies [[org.clojure/clojure "1.3.0"]
[ring "1.0.2"]
[compojure "1.0.1"]
[ring-clj-params "0.1.0"]]
:main awesome-app)
Next, create a file in src
called my_awesome_service.clj
with the following:
(ns my-awesome-service
(:use compojure.core)
(:use ring.middleware.clj-params))
(defn generate-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/clojure"}
:body (print-str data)})
(defroutes handler
(GET "/" []
(generate-response {:hello :cleveland}))
(PUT "/" [name]
(generate-response {:hello name})))
(def app
(-> handler
wrap-clj-params))
And finally, create another file in src
named awesome_app.clj
with the following:
(ns awesome-app
(:use ring.adapter.jetty)
(:require [my-awesome-service :as awe]))
(defn -main
[& args]
(run-jetty #'awe/app {:port 8080}))
Run this app in your console with lein run
and test with curl
using the following:
$ curl -X GET http://localhost:8080/
#=> {:hello :cleveland}
$ curl -X PUT -H "Content-Type: application/clojure" \
-d '{:name :barnabas}' \
http://localhost:8080/
#=> {:hello :barnabas}%
The Clojure *eval-read*
functionality is turned off and trying to use it will result in a server-side exception thus resulting in a nil
return for the eval form; such as:
$ curl -X PUT -H "Content-Type: application/clojure" \
-d '{:expr #=(+ 1 2)}' \
http://localhost:8080/
#=> {:hello nil}
I'm currently only supporting data literals for Clojure versions 1.3 and below. Support for later versions (including tagged literal support) is planned.
Thanks to Mark McGranaghan for his work on Ring and ring-json-params on which this project was based.
Copyright (C) 2012 Fogus
Distributed under the Eclipse Public License, the same as Clojure.