Coder Social home page Coder Social logo

ring-clj-params's Introduction

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.

ring-clj-params's People

Contributors

fogus avatar

Stargazers

Ulrik Sandberg avatar

Watchers

Ulrik Sandberg avatar James Cloos avatar

Forkers

mfikes

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.