8
votes

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" ?

2
If you have found the solution make sure to mark the answer correct.Ankur

2 Answers

11
votes

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

6
votes

The whole request map can be specified in the bindings using :as keyword in bindings and then used to read headers or body :

(POST ["/api/v1/:stor/:txn/data/:file" :file #".*"] 
      [stor txn file :as req] 
      (my-handler stor txn file req))