Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compojure get request when route has binds

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.

like image 825
weeanon Avatar asked Aug 26 '26 08:08

weeanon


1 Answers

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.

like image 108
Joost Diepenmaat Avatar answered Aug 29 '26 13:08

Joost Diepenmaat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!