Question

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

Was it helpful?

Solution

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>
  }

}

OTHER TIPS

I'm not sure, but can you try with

S.param("param_name")

http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html

or with the req class

case r @ JsonPost("some" :: "path" :: _, json) => _ => {
   r.param("name")
}

http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html

Edit: I have this sample running :

package code.rest

import net.liftweb.http.rest._

object SampleRest extends RestHelper {
  serve {
    case Get("sample" :: _, req) =>
        <hello>{req.param("name") getOrElse ("??") }</hello>
  }
}

In snippets, Get and Post parameters are part of the snippet lifecycle. Lift attributes a GUID to the function passed to SHtml.text(defaultValue, passedFunction) and returns places that GUID in the name attribute of the generated HTML element. When the form is submitted, Lift looks up the GUID in the function table and calls the function with the passed parameter.

For more general requests, open the Box:

val q = S.param("named_parameter") openOr ""

and you could set a session variable for stateful requests:

object myObject extends SessionVar[Box[Model]](Empty)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top