Compojure params etc.

153 views
Skip to first unread message

Robert Beaupre

unread,
Apr 5, 2011, 4:11:10 PM4/5/11
to Compojure
Hi there,
This is a very elementary question and if it has been covered in depth
elsewhere, please let me know. Many of the examples I have seen are
for simple apps and I have not really gotten the whole concept.
Thanks for any help you can offer.

Essentially, how do parameters work for handlers?

If I do:

(POST "/foo/:bar/baz/:boo" [bar boo] (some-function....)) of course
that works

is there a way to also get the parameter map or the request map in
this situation? I have tried various combinations of:

(POST "/foo/:bar/baz/:boo" [bar boo params request] (some-
function....))

and params and request are nil

The other way I have seen it done in examples is:

(POST "/foo/:bar/baz/:boo" {params :params} (some-function....))

and that works fine but I cannot seem to get any other things to work
in that situations like:

POST "/foo/:bar/baz/:boo" {params :params request :request} (some-
function....))

here request turns out to be nil

I am using wrap-params, wrap-nested-params, and wrap-keyword params in
my app declaration.

So, my question is, what are the rules for [] and {} ways of getting
parameters and what possible information can I get in those two
forms.

Thanks a lot.
Robert

James Reeves

unread,
Apr 5, 2011, 4:32:16 PM4/5/11
to comp...@googlegroups.com
On 5 April 2011 16:11, Robert Beaupre <codewis...@gmail.com> wrote:
> So, my question is, what are the rules for [] and {} ways of getting
> parameters and what possible information can I get in those two
> forms.

The {} form is normal Clojure destructuring. There's a detail
description of the syntax on the Clojure home page:

http://clojure.org/special_forms#Special Forms--(let [bindings* ] exprs*)

For instance, if you want the parameters and the request map, you can
use the following binding:

{params :params :as request}

This will work in let-forms and fn arguments as well as in Compojure routes.

The [] form is a Compojure-specific destructuring, loosely based on
the Clojure kind.

To bind normal parameters, you can just use:

[x y z]

If you want to bind all remaining unbound parameters, you can use "&":

[x y & z]

This is equivalent to:

(let [x (-> req :params :x)
y (-> req :params :y)
z (dissoc (req :params) :x :y)]
...)

You can also just use:

[& params]

To bind all of the parameters to a map.

Finally, you can use the same :as keyword as in normal Clojure destructuring:

[& params :as request]

This is equivalent to the Clojure destructing:

{params :params :as request}

You can also embed Clojure destructuring in Compojure's destructuring, e.g.

[x y z :as {session :session}]

However, normal parameter symbols can't have embedded destructuring,
because Compojure needs to know the name of the symbol to find the
corresponding parameter. For example, this won't work:

[x [y z]]

Because Compojure won't know which parameter [y z] should be bound to.

I should really put all this on the wiki. I thought I had, but it
turns out I was mistaken. I'll try and get something up today.

- James

Robert Beaupre

unread,
Apr 5, 2011, 5:22:07 PM4/5/11
to Compojure
Hi James,
Thanks a lot, that was a big help. I am fairly new to Clojure so some
of these things aren't obvious to me yet. Thanks for your help and
your hard work on Compojure.

Robert

On Apr 5, 1:32 pm, James Reeves <jree...@weavejester.com> wrote:
> On 5 April 2011 16:11, Robert Beaupre <codewise.rob...@gmail.com> wrote:
>
> > So, my question is, what are the rules for [] and {} ways of getting
> > parameters and what possible information can I get in those two
> > forms.
>
> The {} form is normal Clojure destructuring. There's a detail
> description of the syntax on the Clojure home page:
>
>  http://clojure.org/special_forms#SpecialForms--(let [bindings* ] exprs*)
Reply all
Reply to author
Forward
0 new messages