如何从 Scala Play 访问帖子数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11212248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:17:18  来源:igfitidea点击:

How do I access post data from scala play?

scalaplayframework

提问by user1435853

I have a route that is type "POST". I am sending post data to the page. How do I access that post data. For example, in PHP you use $_POST

我有一个类型为“POST”的路由。我正在向页面发送帖子数据。我如何访问该帖子数据。例如,在 PHP 中你使用 $_POST

How do I access the post data in scala and play framework?

如何访问 Scala 和 Play 框架中的帖子数据?

回答by Leonya

As of Play 2.1, there are two ways to get at POST parameters:

从 Play 2.1 开始,有两种方法可以获取 POST 参数:

1) Declaring the body as form-urlencoded via an Action parser parameter, in which case the request.body is automatically converted into a Map[String, Seq[String]]:

1) 通过 Action 解析器参数将 body 声明为 form-urlencoded,在这种情况下 request.body 会自动转换为 Map[String, Seq[String]]:

def test = Action(parse.tolerantFormUrlEncoded) { request =>
    val paramVal = request.body.get("param").map(_.head)
}

2) By calling request.body.asFormUrlEncoded to get the Map[String, Seq[String]]:

2) 通过调用 request.body.asFormUrlEncoded 来获取 Map[String, Seq[String]]:

def test = Action { request =>
    val paramVal = request.body.asFormUrlEncoded.get("param").map(_.head)
}

回答by biesior

Here you've got good sample how it's done in Play:

在这里,您有很好的示例,它是如何在 Play 中完成的:

https://github.com/playframework/Play20/blob/master/samples/scala/zentasks/app/controllers/Application.scala

https://github.com/playframework/Play20/blob/master/samples/scala/zentasks/app/controllers/Application.scala

val loginForm = Form(
  tuple(
    "email" -> text,
    "password" -> text
  ) verifying ("Invalid email or password", result => result match {
    case (email, password) => User.authenticate(email, password).isDefined
  })
)



/**
 * Handle login form submission.
 */
def authenticate = Action { implicit request =>
  loginForm.bindFromRequest.fold(
    formWithErrors => BadRequest(html.login(formWithErrors)),
    user => Redirect(routes.Projects.index).withSession("email" -> user._1)
  )
}

It's described in the documentation of the forms submission

它在表单提交的文档中进行了描述

回答by virtualeyes

as @Marcus points out, bindFromRequest is the preferred approach. For simple one-off cases, however, a field

正如@Marcus 指出的那样, bindFromRequest 是首选方法。然而,对于简单的一次性案例,一个字段

<input name="foo" type="text" value="1">

can be accessed via post'd form like so

可以像这样通过post'd表单访问

val test = Action { implicit request =>
  val maybeFoo = request.body.get("foo") // returns an Option[String]
  maybeFoo map {_.toInt} getOrElse 0
}

回答by Arvind Audacious

Here you've got good sample how it's done in Play 2:

在这里你有很好的示例,它是如何在 Play 2 中完成的:

def test = Action(parse.tolerantFormUrlEncoded) { request =>
    val paramVal = request.body.get("param").map(_.head).getorElse("");
}