3

I use to define my routes in composure like this:

(GET "/home" [req] (home-page req))

and then I have the whole request object available to use inside my handler.

but now I want to use routes with binds, like:

(GET "/details/:id" [id] (details-page id))

in this case, it seems that I have no way to get the request AND the bound arguments at the same time. I tried:

(GET "/details/:id" [id req] (details-page id req))

but req comes nil.

is there any way to get the request on routes with bindings?

I want the bindings so I don't have to do things like:

(GET "/details" [req] (details-page req)) and then have <a href="/details?id=123">...

and I need the request to have access to the session and request headers.

any suggestion?

thanks in advance.

weeanon
  • 821
  • 10
  • 17

3 Answers3

4

Compojure's vector-destructuring of bindings is optimized for params and not very flexible, but luckily you can use normal map-based destructuring of the request for the trickier cases:

(GET "/details/:id" {:keys [id] :params :as req} (details-page id req))

Should work.

Joost Diepenmaat
  • 17,633
  • 3
  • 44
  • 53
1

hum... it is not perfect, but I'm going with:

(GET "/details/:id" req (details-page (-> req :params :id) req))

this snippet works, and solves my problem, but I would love something simpler (DRY).

weeanon
  • 821
  • 10
  • 17
1

According to https://github.com/weavejester/compojure/wiki/Destructuring-Syntax, you should be able bind req to the entire request by adding :as req to the binding vector:

(GET "/details/:id" [id :as req] (details-page id req))

Nathan Davis
  • 5,636
  • 27
  • 39