Compojure get request when route is associated
I use to define routes in this mode:
(GET "/home" [req] (home-page req))
and then I have the entire request object available for use inside my handler.
but now I want to use anchor routes like:
(GET "/details/:id" [id] (details-page id))
in this case, it seems that I cannot get the AND query and associated arguments at the same time. I tried:
(GET "/details/:id" [id req] (details-page id req))
but req comes up to zero.
is there a way to get a request for anchor routes?
I need bindings so I don't need to do things like:
(GET "/details" [req] (details-page req))
, and then <a href="/details?id=123">...
and I need a request to access the session and request headers.
any suggestion?
early.
hum ... it's not perfect, but I'm going to:
(GET "/details/:id" req (details-page (-> req :params :id) req))
this snippet works and solves my problem, but I would like something simpler (DRY).
The layout of the vector destructuring bindings is optimized for parameters and not very flexible, but luckily you can use the standard destructuring of the query on queries for more complex cases:
(GET "/details/:id" {:keys [id] :params :as req} (details-page id req))
Must work.
According to https://github.com/weavejester/compojure/wiki/Destructuring-Syntax you should bind req
to the whole request by adding :as req
bindings to the vector:
(GET "/details/:id" [id :as req] (details-page id req))