How do 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 the http headers to the txn-add handler. I did a lot of google and looked at the code but did not find anything useful.

I know, I can use the following to pass the headers (but then it does not parse the url request)

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

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

+6
source share
2 answers

If the second argument is GET, POST, etc. is not a vector, it is a destructive form of binding for request . This means that you can do things like:

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

To highlight the parts of request that you want. See Ring SPEC and Clojure docs for binding and destruction.

+8
source

The entire request map can be specified in bindings using the :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)) 
+6
source

Source: https://habr.com/ru/post/945654/


All Articles