Playframework 处理发布请求 [英] Playframework handling post request

查看:22
本文介绍了Playframework 处理发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的路线中:

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

为该路由定义了一个控制器方法:

There is a controller method defined for that route:

def validateForm(jsonForm:String) = Action { ...

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

Then I try to send POST request by chrome POSTMAN plugin (see pic above).

我使用:

url: http://localhost:9000/forms/FormValidator1/validateForm
标题: 内容类型:应用程序/json
json 数据: {name: "me", surname: "my"}

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

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

So, sending this POST request I can not reach controller's method by mentioned route / url. Why?

更新:

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

Interestly enough: after I got it working on my laptop (see my answer below) then push it on gitHub and pull it to another machine it starts working differently. Now it complains than Bad Request is [Invalid XML] nevertheless I use "application/json" header and did not change any line of code after commit. I wonder maybe it is a bug.

推荐答案

看来我明白了.

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

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

错误正确方式说明:

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

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

This is the way how POST params are passing..in play. (second link is explanation WHY):

有时您必须做出妥协.在 Play 1 你可以绑定你的来自从 URL 路径中提取的任何参数的操作参数,查询字符串甚至请求正文.它的生产力很高,但是您无法控制表单的上传方式.我的意思是,如果一个用户上传了一个你需要加载整个请求的大文件内存能够处理它.

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.

在 Play 2 中,您可以控制请求正文的提交.你可以拒绝如果用户有问题,尽早处理,您可以处理大文件或流,而无需用多个 HTTP 填满您的内存块……你可以高度控制发生的事情,它可以帮助你扩展您的服务.但是,硬币的另一面是,当请求被路由,Play 2 只使用请求头来使其决定:请求正文尚不可用,因此无法从提取的参数中直接绑定动作参数请求正文.

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 并将其拉到另一台机器上,它开始以不同的方式工作.现在它抱怨 Bad Request[Invalid XML] 不过我使用 "application/json" 标头并且没有更改任何代码行提交.

UPDATE: Interestly enough: after I got it working on my laptop then push it on gitHub and pull it to another machine it starts working differently. Now it complains than Bad Request is [Invalid XML] nevertheless I use "application/json" header and did not change any line of code after commit.

更新 2

所以我是这样修复的:

在角度方面(我们甚至可以注释dataTypeheaders):

On angular side (we even can comment dataType and headers):

  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

这篇关于Playframework 处理发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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