Shanti Adhikari
Shanti Adhikari

Reputation: 91

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

Upvotes: 8

Views: 3553

Answers (2)

Ankur
Ankur

Reputation: 33637

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

Upvotes: 6

Joost Diepenmaat
Joost Diepenmaat

Reputation: 17773

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

Upvotes: 11

Related Questions