How is it possible to simply access to get and post attributes in lift framework inside RestHelper? There are no any explicit examples about it in documentation :(
package my.domain
import net.liftweb.http._
import net.liftweb.http.rest._
import net.liftweb.json.JsonAST._
import net.liftweb.json._
import net.liftweb.common.{Box,Full,Empty,Failure,ParamFailure}
import net.liftweb.mapper._
import ru.dmteam.model.{RssItem}
object ContentRest extends RestHelper {
def getq: String = {
val q = S.param("q")
q.toString
}
serve {
case "api" :: "static" :: _ XmlGet _=> <b>{getq}</b>
}
}
I want to understand how to make lift show value of q
when I am requesting http://localhost:8080/api/static.xml?q=test
Lift uses Box rather than null to indicate if a parameter was passed. This allows the nice use of Scala's for comprehension to chain together a nice request handler. I'll let the code speak for itself:
object MyRest extends RestHelper {
// serve the q parameter if it exists, otherwise
// a 404
serve {
case "api" :: "x1" :: _ Get _ =>
for {
q <- S.param("q")
} yield <x>{q}</x>
}
// serve the q parameter if it exists, otherwise
// a 404 with an error string
serve {
case "api" :: "x2" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing"
} yield <x>{q}</x>
}
// serve the q parameter if it exists, otherwise
// a 401 with an error string
serve {
case "api" :: "x2" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing" ~> 401
} yield <x>{q}</x>
}
// serve the q, r, s parameters if this exists, otherwise
// different errors
serve {
case "api" :: "x3" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing" ~> 401
r <- S.param("r") ?~ "No 'r'" ~> 502
s <- S.param("s") ?~ "You're s-less" ~> 404
} yield <x><q>{q}</q><r>{r}</r><s>{s}</s></x>
}
}
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