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
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()
.
ctx()._requestHeader().getQueryString("q").get();
this doesn't require any extra imports and should work straight out of bootstrapped controller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With