Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to you access :headers inside compojure function

org.clojure/clojure-contrib "1.2.0" ring "1.1.8" compojure "1.1.5" clout "1.1.0"

(defroutes rest-routes
    (GET "/" [] "<p> Hello </p>")
    (POST "/api/v1/:stor/sync" [stor] (start-sync stor))
    (POST ["/api/v1/:stor/:txn/data/:file" :file #".*"] [stor txn file] (txn-add stor txn file))
    (ANY "*" [] "<p>Page not found. </p>"))

In the second POST, I also want to pass all http-headers to "txn-add" handler. I did lot of google and look through the code, but couldn't find anything useful.

I know, I can use the following to pass headers (but then it doesn't parse url request),

(POST "/api/v1"
  {headers :headers} (txn-add "dummy stor" "dummy txn" headers))

Also, how do I pass the content (i.e. :body) of POST request to "txn-add" ?

like image 834
Shanti Adhikari Avatar asked May 23 '13 05:05

Shanti Adhikari


1 Answers

If the second argument to GET, POST etc is not a vector, it's a destructuring binding form for request. That means you can do things like:

(GET "/my/path"
   {:keys [headers params body] :as request} 
   (my-fn headers body request))

To pick out the parts of request you want. See the Ring SPEC and Clojure's docs on binding & destructuring

like image 110
Joost Diepenmaat Avatar answered Sep 30 '22 04:09

Joost Diepenmaat