14
 case GET(Path("/rtb_v1/bidrequest")) => Action {  implicit request =>

I want to take the request object above and get all of the key/value pairs sent in the form post and flatten it into a Map[String,String]

i have gone through all the documents and am at a dead end.

This is pretty freaking easy in Java/Servlets I;m wondering why there is no documentation on a simple thing like this anywhere..

Map<String, String[]> parameters = request.getParameterMap();
Ryan Medlin
  • 199
  • 1
  • 2
  • 7

4 Answers4

27

Play's equivalent of request.getParamterMap is request.queryString, which returns a Map[String, Seq[String]]. You can flatten it to a Map[String, String] with

request.queryString.map { case (k,v) => k -> v.mkString }

And here is the documentation.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
9

As an alternative to the way that Kim does it, I personally use a function like..

def param(field: String): Option[String] = 
  request.queryString.get(field).flatMap(_.headOption)
Ivan Meredith
  • 2,222
  • 14
  • 13
2

It won't work if request is using POST method. Following code can be used:

req.body match {
  case AnyContentAsFormUrlEncoded(params) ⇒
    println(s"urlEncoded = $params")
  case mp @ AnyContentAsMultipartFormData(_) ⇒
    println(s"multipart = ${mp.asFormUrlEncoded}")
}
cchantep
  • 9,118
  • 3
  • 30
  • 41
0

You might have to use the following:

request.queryString.map { case (k,v) => k -> v.mkString }).toSeq: _*
Louis Querel
  • 586
  • 10
  • 12