在Play Framework中使用POST路由参数 [英] Using POST routes parameters in Play Framework

查看:116
本文介绍了在Play Framework中使用POST路由参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录路由,应该将其输入参数作为POST变量进行传输.我已经定义了这样的路线:

POST    /v1/accounts/login          controllers.v1.Accounts.login(username: String, password: String)

我的控制器是这样的:

object Accounts extends Controller {
  def login(username: String, password: String) = Action {
    Ok("Foo " + username)
  }
}

当我使用Chrome浏览器测试此路由时,高级REST客户端它仅适用于GET参数,不适用于application/x-www-form-urlencoded POST表单数据.

Play框架文档从未真正提及POST参数,但也没有提及它不起作用.

我认为,如果我创建一个Form并将我的请求绑定到该表单,它可能会起作用,但这似乎不必要地复杂.

真的没有办法使用路由文件绑定POST参数吗?

解决方案

Route =解决URL中的参数=通过GET发送参数.

这意味着,您正在尝试通过... GET params ...来发送POST请求,这是什么意思?

James Roper 解释:

在路由时,Play尚未消耗请求正文,因此也未解析提交的表单. -并且您也不希望这样做,因为是由您的操作决定如何/是否将请求正文进行解析,流传输,发送到其他地方,如果Play在路由时执行此操作,则会限制您在操作中可以执行的操作.

从安全角度考虑,将凭据保留在客户端路径中每台计算机的日志中绝对不是一个好主意.

相反,您应该使用常见的表单处理方式(如基本表单文档中所述):

路线:

POST    /v1/accounts/login      controllers.v1.Accounts.login

动作:

val userForm = Form(
  tuple(
    "username" -> text,
    "password" -> text
  )
)

def login = Action { implicit request =>
  val (username, password) = userForm.bindFromRequest.get
  Ok("Hello " + username + ", you're trying to login with: " + password)
}

I have a login route that should transmit its input parameters as POST variables. I have defined my route like this:

POST    /v1/accounts/login          controllers.v1.Accounts.login(username: String, password: String)

and my Controller is like this:

object Accounts extends Controller {
  def login(username: String, password: String) = Action {
    Ok("Foo " + username)
  }
}

When I test this route using Chromes Advance REST Client it only works for GET parameters and not if I send it as application/x-www-form-urlencoded POST form data.

The Play Framework documentation never actually mentions POST parameters but neither does it say that it does not work.

I think it might get it to work if I create a Form and bind my request to that but that seems needlessly complex.

Is there really no way to bind POST parameters using the routes file?

解决方案

Route = resolving params inside the URL = sending params via GET.

That means, that you are trying to send POST request by... GET params... where's the sense ?

James Roper explains that:

At routing time, Play hasn't yet consumed the request body, and so hasn't parsed the submitted form. - and you don't want it to either, because it's your action that decides how/whether the request body gets parsed, streamed, sent elsewhere, if Play did this at routing time, it would limit what you could do in an action.

From security point of view it's definitely bad idea to leave credentials in logs of every machine in the path of client.

Instead you should do it with common form handling way like described in base form documentation:

route:

POST    /v1/accounts/login      controllers.v1.Accounts.login

action:

val userForm = Form(
  tuple(
    "username" -> text,
    "password" -> text
  )
)

def login = Action { implicit request =>
  val (username, password) = userForm.bindFromRequest.get
  Ok("Hello " + username + ", you're trying to login with: " + password)
}

这篇关于在Play Framework中使用POST路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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