我正在使用 Play 2.1.0。我正在尝试创建一个接受 POST 请求并要求请求正文中需要布尔值的操作。现在发生的情况是,即使我不提供参数,Action 也会得到一个错误的值。测试代码:
// /post/boolean/single routes to this method
def postTestBooleanSingle = Action { implicit request =>
val form = Form("flag" -> boolean)
form.bindFromRequest.fold(
formWithErrors =>
BadRequest(formWithErrors.errors map (fe => fe.key + ": " + fe.message) mkString ", "),
flag => Ok(f"got $flag%b")
)
}
// /post/num/single routes to this method
def postTestNumSingle = Action { implicit request =>
val form = Form("num" -> number)
form.bindFromRequest.fold(
formWithErrors =>
BadRequest(formWithErrors.errors map (fe => fe.key + ": " + fe.message) mkString ", "),
num => Ok(f"got $num%d")
)
}
$ curl -XPOST -d "num=42" http://localhost:9000/post/num/single
got 42
$ curl -XPOST http://localhost:9000/post/num/single
num: error.required // didn't provide num so i get an error
$ curl -XPOST -d "flag=true" http://localhost:9000/post/boolean/single
got true
$ curl -XPOST http://localhost:9000/post/boolean/single
got false // ???
我如何需要一个布尔参数?