Re: How to build these routes

80 views
Skip to first unread message

James Reeves

unread,
Apr 16, 2013, 7:36:22 AM4/16/13
to Compojure
Compojure's routes don't match against the query string directly, but you can make a route not match by returning nil from it.

So the first URL could be handled using a route like:

    (defn digits? [s]
      (re-matches #"\d+" s))

    (GET "/" [foo bar]
      (if (and (digits? foo) (digits? bar))
        ...))

And the second with something like:

    (GET "/" [id]
      (if (and (vector? id) (map digits? id))
        ...))

However, it would make more sense for you to return a 400 response if the user supplies bad parameters, rather than just not matching the route.

- James


On 16 April 2013 09:07, Ryan T. <areka...@gmail.com> wrote:
Hello all,

I am trying to understand how to build the routes for the following urls but it seems that I can't quite get them right:

The first one is:

http://www.example.com?foo=1&bar=100

The above is a GET request with two query parameters, foo and bar and both of them should only accept numeric values.

And the second url is:

http://www.example.com?id[]=1&id[]=5&id[]=7

The above is a GET request again, with an unknown number of id parameters which should only accept numeric values.

Can someone explain to me how to define those routes with defroutes and add regex for them to accept only the values I want?

Thank you for your time

--
You received this message because you are subscribed to the Google Groups "Compojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to compojure+...@googlegroups.com.
To post to this group, send email to comp...@googlegroups.com.
Visit this group at http://groups.google.com/group/compojure?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ryan T.

unread,
Apr 16, 2013, 8:44:43 AM4/16/13
to comp...@googlegroups.com, ja...@booleanknot.com
However, it would make more sense for you to return a 400 response if the user supplies bad parameters, rather than just not matching the route.

I agree with that and that's what am I going to do. I just wanted to check if compojure offered a way that I missed :)

Thanks James 

Ryan T.

unread,
Apr 16, 2013, 11:14:04 AM4/16/13
to comp...@googlegroups.com, ja...@booleanknot.com
While we are at it, is there some recommended way with compojure to validate query parameters? 


On Tuesday, April 16, 2013 2:36:22 PM UTC+3, James Reeves wrote:
Reply all
Reply to author
Forward
0 new messages