Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the params of a POST request with Compojure

According to the Compojure documentation on routes, I can easily get individual parameters like this:

(POST "/my-app" [param1 param2]
  (str "<h1>Hello " param1 " and " param2 "</h1>"))

How do I get all parameters, not just individual parameters?

like image 278
at. Avatar asked Aug 29 '15 09:08

at.


2 Answers

compojure handlers receive the entire request map as their argument, so handler has also an access to all of the parameters. For example, to see entire request:

(POST "/" request
    (str request))

or, to extract all form parameters:

(POST "/" request
    (str (:form-params request)))

The syntax used in the question is a compojure-specific destructuring syntax, which allows extracting individual parameters from the request. This is similar to clojure's usual destructuring syntax, and, as with usual destructuring, compjure's destructuring also allows mixing the destructuring and still getting the entire request:

(POST "/" [param1 param2 :as request]
        (str (:form-params request)))

or, extracting named and all "additional" parameters:

(POST "/" [param1 param2 & more-params]
        (str more-params))
like image 72
Timur Avatar answered Nov 17 '22 07:11

Timur


I just guessed to put & params in the vector and that worked:

(POST "/my-app" [& params]
  (str "<h1>Hello " params "</h1>"))
like image 4
at. Avatar answered Nov 17 '22 05:11

at.