Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.params is gone in Play Framework 2.0

Is there a way to access all request parameters, regardless of HTTP method? I have poured over the documentation and the api without finding a way in Play Framework 2.0.

I have a search on a site that accepts POST and GET. The custom tracking on the site examines all parameters passed in to determine the correct way to store tracking data.

In the Play Framework 1.2.x, I was able to access parameters from a request in a Controller with

request.params.get("keywords")
request.params.get("location") 
request.params.all()

With Play Framework 2.0, this is no longer the case. The Request no longer has the method params, only queryString and queryString only works with GET and not POST.

It is not feasible to define every single possible tracking parameter into the Controller Action, they are dynamic.


UPDATE: A possible work around is using Body Parsers.

Depending on the content type of the request, the appropriate parser is used, e.g. application/form-url-encoded vs application/json

This is the crude Map that combines POST parameters and GET parameters, with GET parameters taking precedence.

val params: collection.mutable.Map[String, Seq[String]] = collection.mutable.Map() 
params ++= request.body.asFormUrlEncoded.getOrElse[Map[String, Seq[String]]] { Map.empty } 
params ++= request.queryString 
like image 402
mguymon Avatar asked Mar 21 '12 16:03

mguymon


2 Answers

The Play 2.0 documentation doesn't explain this very well. Body parsers are an acceptable solution, but I've found DynamicForm to be much more lightweight and user friendly.

The documentation can be found here.

In particular, the DynamicForm.bindFromRequest() is a good place to start if you are trying to replace the old Play 1.0 request.params.get().

like image 129
44maagnum Avatar answered Nov 20 '22 15:11

44maagnum


ctx()._requestHeader().getQueryString("q").get();

this doesn't require any extra imports and should work straight out of bootstrapped controller.

like image 34
Ilya Avatar answered Nov 20 '22 16:11

Ilya