params :id matching

36 views
Skip to first unread message

Tommy Chheng

unread,
Oct 13, 2011, 8:23:04 PM10/13/11
to scalat...@googlegroups.com
I setup a route like:
  get("/sweble/:id"){
    println(params("id"))
  }

With a string like "test", it works, but with strings with '/', it doesn't.  "F/A-18_Hornet" doesn't get passed in.

How can I make this work?

Ross A. Baker

unread,
Oct 13, 2011, 11:58:43 PM10/13/11
to scalat...@googlegroups.com

Why it doesn't work: a named parameter matches everything but '/', '?', and '#'.

Why it doesn't work even if you URL encode the special characters: the
servlet container decodes the special characters, so we lose the
distinction between "/" and "%2f". This behavior is helpful in the
case of diacritics, but unhelpful in the case of punctuation. We have
a ticket open for this, but haven't agreed on the cleanest way to fix
it: https://github.com/scalatra/scalatra/issues/52

Possible workarounds:

1) get("/sweble/*"): the asterisk matches everything. The result is
in params("splat").

2) get("""/sweble/.*""".r): a regex gives you more control if the
splat is too aggressive. Captured groups are in
multiParams("captures").

3) override requestPath based on request.getRequestURI. The servlet
container does not decode that property, but you're on your own for
stripping context paths, decoding parameter values, etc. You're
essentially fixing Issue #52 yourself if you do this.

--
Ross A. Baker
ba...@alumni.indiana.edu
Indianapolis, IN, USA

Reply all
Reply to author
Forward
0 new messages