Reputation: 18148
I am refactoring a project to use Play Framework instead of Scalatra, and am running into trouble with query parameters containing semicolons, e.g. /url?filter=filter1:val1;filter2:val2 gets mapped to filter = "filter1:val1;filter2:val2" in Scalatra, but gets mapped to filter = "filter1:val1" in Play Framework. The only solution I've found, Escape semicolon route playframework 2.0.2, suggests using a regex to capture the entire parameter, but this appears to only be applicable to path parameters and not to query parameters - one of my requirements is that I can't change the way that the front end calls the api, i.e. I can't change a query parameter into a path parameter.
How can I tell Play not to parse out the semicolons in query parameters, i.e. to return "filter1;filter2" instead of "filter1"?
I can manually parse out the query parameters using a regex on "request.rawQueryString", but I'd rather avoid this if possible.
Upvotes: 6
Views: 755
Reputation: 18148
The solution we went with was to parse out the query parameters using an implicit class, which wasn't as onerous as I thought it would be
implicit class ParsedRawQueryString[+T](req: Request[T]) {
import java.net.URLDecoder.decode
def parsedRawQueryString = (for {
s <- decode(req.rawQueryString, "UTF-8").split('&') if s.contains('=')
} yield {
val index = s.indexOf('=')
s.substring(0, index) -> s.substring(index + 1)
}).toMap
}
def route = Action {
request =>
val queryParams = request.parsedRawQueryString
}
Upvotes: 3