gpt4 book ai didi

json - Playframework 处理发布请求

转载 作者:行者123 更新时间:2023-12-03 16:55:25 24 4
gpt4 key购买 nike

在我的 routes :

POST        /forms/FormValidator1/validateForm                       controllers.FormValidator1.validateForm(jsonForm:String)

为该路由定义了一个 Controller 方法:
def validateForm(jsonForm:String) = Action { ...

然后我尝试通过 chrome POSTMAN 插件发送 POST 请求(见上图)。

我用:

url: http://localhost:9000/forms/FormValidator1/validateForm
headers: Content Type: application/json
json data: {name: "me", surname: "my"}



因此,发送此 POST 请求时,我无法通过提到的路由/url 访问 Controller 的方法。 为什么?

更新:

有趣的是:在我的笔记本电脑上使用它后(请参阅下面的答案),然后将它推送到 gitHub 并将其拉到另一台机器上,它开始以不同的方式工作。现在它比 更提示错误请求 是 [无效的 XML] 不过我使用 "application/json"提交后没有更改任何代码行。我想知道它可能是 错误 .

enter image description here

最佳答案

看来我明白了。

这里:
https://groups.google.com/forum/#!topic/play-framework/XH3ulCys_co

和这里:
https://groups.google.com/forum/#!msg/play-framework/M97vBcvvL58/216pTqm22HcJ

wrongcorrect方式解释:

Doesn't work: curl -d "name=sam" http://localhost:9000/test
Works: curl -d "" http://localhost:9000/test?name=sam

这就是 POST 参数的传递方式……在游戏中。 (第二个链接是解释为什么):

Sometimes you have to make compromises. In Play 1 you could bind your action parameters from any parameter extracted from the URL path, query string or even the request body. It was highly productive but you had no way to control the way the form was uploaded. I mean, if a user uploads a big file you needed to load the entire request in memory to be able to handle it.

In Play 2 you can control the request body submission. You can reject it early if something is wrong with the user, you can process big files or streams without filling your memory with more than one HTTP chunk… You gain a high control of what happens and it can help you to scale you service. But, the other side of the coin is that when a request is routed, Play 2 only uses the request header to make its decision: the request body is not available yet, hence the inability to directly bind an action parameter from a parameter extracted from the request body.



更新:
有趣的是:在我的笔记本电脑上运行它之后,将它推送到 gitHub 并将其拉到另一台机器上,它开始以不同的方式工作。现在它比 更提示错误请求 [无效的 XML] 尽管如此,我还是使用 "application/json"提交后没有更改任何代码行。

更新 2

所以我是这样修复的:

在角度方面(我们甚至可以评论 dataTypeheaders ):
  var data =  $scope.fields

$http({
url: '/forms/FormValidator1/validateForm',
method: "POST",
//dataType: "json",
data: data,
//headers: {'Content-Type': 'application/json'}
}).success(function (data, status, headers, config) {
console.log("good")
}).error(function (data, status, headers, config) {
console.log("something wrong")
});

在 playFramework 方面:(使用 BodyParser )
 def validateForm = Action { request =>


val body: AnyContent = request.body
val jsonBody: Option[JsValue] = body.asJson

// Expecting text body
jsonBody.map { jsValue =>

val name = (jsValue \ "name")
val surname = (jsValue \ "surname")
....
}

路由(根本不定义参数!):
 POST       /forms/FormValidator1/validateForm             controllers.FormValidator1.validateForm

关于json - Playframework 处理发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19941786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com