I want to know the request.body.asFormUrlEncoded
contains deviceId
or not.
val formValues=request.body.asFormUrlEncoded
val number = formValues.get("mobile").head
var deviceId ="deviceIdNotFound"
if(condtion) //thats the problem
deviceId= formValues.get("deviceId").head
is there any way of conatins or any other function for Option[Map[String,Seq[String]]]
I'd strongly encourage you not to use
formValues.get("whatever")
, in part because the syntax is highly confusing—it looks like you're callingget
with a key argument (as for example on a map), when really you're callingget
on theOption
(which is an unsafe operation—you should stay away fromget
onOption
basically always) and thenapply
on the resulting map (also unsafe). This muddle is Scala's fault, not yours, but you still want to avoid stepping in it.Instead you can use
exists
on theOption
together withcontains
on the map. Here's a slightly simplified example:This will return
true
only if theOption
is non-empty and the map it contains has the key.An even better approach is to avoid the
if
-statement like this:Here we end up with an
Option
that contains the value pointed to bykey
if the originalOption
is non-empty and the map contains that key.