tags:

views:

177

answers:

3

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

+3  A: 

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>
  }
}
jgoday
I have updated the post.S.param("q").toString is already returning "Empty"...
SMiX
+1  A: 

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)

nunobaba
I have no any html code. I am writing json/xml-rest service and I need to be able to read parameters manually sent to my lift application.
SMiX
In this case, the poster was looking to get request parameters as part of REST handling. While Lift's form generation code will generate code with unique GUIDs for form elements (you can do it the old fashioned way if you want), that's not applicable to REST calls where the API defines the query parameters. The question was how to get those query parameters. S.param is the right answer, but it returns a Box[String] which will be Empty if the query parameter was not supplied or Full(value) if it was.
David Pollak
+1  A: 

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

}
David Pollak
nice example! Thanks.
SMiX