Skip to content

ulsa/ring-clj-params

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ring-clj-params

A Ring middleware that augments :params according to a parsed Clojure data literal request body.

Where

Usage

Leiningen

In your :dependencies section add the following:

[ring-clj-params "0.1.0"]

Ring

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}))

Testing

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.

Acknowledgment(s)

Thanks to Mark McGranaghan for his work on Ring and ring-json-params on which this project was based.

License

Copyright (C) 2012 Fogus

Distributed under the Eclipse Public License, the same as Clojure.

About

Ring middleware that augments :params according to a parsed Clojure data literal request body.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors