在播放框架java中检索POST请求中发送的请求正文字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23034837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:11:36  来源:igfitidea点击:

Retrieve the request body string sent in POST request in play framework java

javapostplayframeworkplayframework-2.0

提问by Bourne

I'm using play framework in Java. I want to retrieve the entire request body sent in a POST request to the play server. How can I retrieve it?

我在 Java 中使用 play 框架。我想检索在 POST 请求中发送到播放服务器的整个请求正文。我怎样才能找回它?

采纳答案by biesior

Take a look into play.mvc.Httpclass, you have some options there (depending on data format) i.e.

看看play.mvc.Http课堂,你有一些选择(取决于数据格式)即

RequestBody body = request().body();
MultipartFormData formData = request().body().asMultipartFormData();
Map<String, String[]> params = request().body().asFormUrlEncoded();
JsonNode json = request().body().asJson();
String bodyText = request().body().asText();

You can test request().body().asText()i.e. using cUrl from commandline:

您可以request().body().asText()从命令行使用 cUrl进行测试:

curl  -H "Content-Type: text/plain" -d  'Hello world !' http://domain.com/your-post-action

... or using some tool, like browser plugin: https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

...或使用一些工具,如浏览器插件:https: //chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

回答by SobiborTreblinka

If you call the following code on a request;

如果您根据请求调用以下代码;

String bodyText = request().body().asText();

bodyText will be null if the Content-Type header is application/json

如果 Content-Type 标头是 application/json,bodyText 将为 null

There isn't a way using the provided controller APIs to just get JSON text if the Content-Type header is application/json without first converting to a JsonNode

如果 Content-Type 标头是 application/json 而不先转换为 JsonNode,则无法使用提供的控制器 API 来获取 JSON 文本

So the best way to do this if the application/json is your Content-Type header is

因此,如果 application/json 是您的 Content-Type 标头,那么执行此操作的最佳方法是

String bodyText = request().body().asJSON().toString();

This is a fail on play framework's part, because they should just have a method to get the request body as a String no matter what the Content-Type header is.

这对 play 框架来说是一个失败,因为无论 Content-Type 标头是什么,他们都应该有一种方法来将请求正文作为字符串获取。

回答by Viktor Aseev

With Play Framework 2.3 it is possible to get raw json text even is Content-Type header is application/json

使用 Play Framework 2.3,即使 Content-Type 标头是 application/json,也可以获得原始 json 文本

def postMethod = Action(parse.tolerantText) { request =>
    val txt = request.body
}

回答by xrs

It will give request JSON body as string. I tested it on play 2.6.x
val body = request.body.asJson.get.toString()

它将以字符串形式提供请求 JSON 正文。我在 play 2.6.x 上测试过
val body = request.body.asJson.get.toString()