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

这一生的挚爱 提交于 2021-01-22 06:59:37

问题


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?


回答1:


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



回答2:


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

(POST "/my-app" [& params]
  (str "<h1>Hello " params "</h1>"))



回答3:


something like this return all the params:

(POST "/test" {params :params} 
    (str "POST params=" params))

use this notation to access a particular parameter:

(println (params :Nom))


来源:https://stackoverflow.com/questions/32284500/how-to-get-all-the-params-of-a-post-request-with-compojure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!