Playframework处理后请求 [英] Playframework handling post request

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

问题描述

在我的routes中:

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
标题::内容类型:application/json
json数据: {名称:我",姓氏:我"}

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

因此,发送此POST请求后,我无法通过提到的route/url到达控制器的方法. 为什么?

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

更新:

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

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

wrongcorrect方式说明:

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

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

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.

在播放2中,您可以控制请求正文的提交.你可以拒绝 如果用户有问题,请尽早处理, 文件或流,而不会用多个HTTP来填充内存 块…您可以高度控制发生的事情,它可以帮助您 扩展您的服务范围.但是,硬币的另一面是 请求被路由,播放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 )

On playFramework side: (use 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天全站免登陆