ps0604
ps0604

Reputation: 1071

Receiving POST submission in Play Framework/Scala REST service

I have looked online and cannot make this work. I just need to receive POST fields submitted from a browser to the server. The form is submitted with a single JSON object.

This is what I've got:

  case class Us (firstName: String, lastName: String)

  def applyUser = Action(BodyParsers.parse.json) {

    val userForm = Form(
      mapping(
        "first" -> text,
        "last" -> text
      )(Us.apply)(Us.unapply)
    )

    val json: JsValue = JsObject(Seq("ret" -> JsString("0")))
    Ok(json)

}

I get the following error: Expression of type Result doesn't conform to expected type (Request[JsValue]) => Result

What is wrong with this code?

Upvotes: 0

Views: 73

Answers (1)

James
James

Reputation: 2764

The Action apply method expects a function rather than a Result object. Try the following:

case class Us (firstName: String, lastName: String)

  def applyUser = Action(BodyParsers.parse.json) { body => //This is the change required to your code

    val userForm = Form(
      mapping(
        "first" -> text,
        "last" -> text
      )(Us.apply)(Us.unapply)
    )

    val json: JsValue = JsObject(Seq("ret" -> JsString("0")))
    Ok(json)

}

Upvotes: 1

Related Questions