如何从Scala Play访问发布数据? [英] How do I access post data from scala play?

查看:69
本文介绍了如何从Scala Play访问发布数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条类型为"POST"的路由.我正在将帖子数据发送到页面.我如何访问该帖子数据.例如,在PHP中,您使用$ _POST

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

如何在scala和play框架中访问帖子数据?

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

推荐答案

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

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

1)通过Action解析器参数将主体声明为表单编码,在这种情况下,request.body会自动转换为Map [String,Seq [String]]:

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]]:

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

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

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

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

这篇关于如何从Scala Play访问发布数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆