Hi I am using the following for my api in Lift:
case "api" :: "all" :: _ Get req => for {
val after <- req.param("after")
val before <- req.param("before")
val limit <- req.param("limit")
} yield JsonResponse(json(ResponseLimitAfterBefore(limit.toInt,
after.toString,
before.toString)))
My issue is if any of the three parameters are missing it gives an error. Can anyone help me with how to assign a value to any of them if any parameter is missing? For example if after is not in the url then how can I assign a default value to after?
Thanks,
-Faran
If seems you do not understand how for comprehensions work within Scala. Consider the following:
Notice that even though
xxhas a value, the result isNone. Given thatreq.paramgives you aBox[String](which is similar to anOption[String]), you could just do something like this (if you want a response whatever params are passed):Or, if you just want to provide a default response overall, rather than a paramterised default response:
I would suggest playing with both LiftResponse subtypes more and also getting a firm grasp on what for comprehensions actually do.
Hope that helps.