简体   繁体   English

如何在没有内容类型标题的情况下从请求访问请求正文

[英]how to access request body from request without content-type header in play

I see this is a tough question for Play. 我认为这对Play来说是一个棘手的问题。 A lot of people asked that question, but still it is not clear how to get bytes from request body if content type is not set in Java. 许多人提出了这个问题,但是如果在Java中未设置内容类型,仍然不清楚如何从请求正文中获取字节。

There is a solution in Scala, but it is not working for my case. Scala中有一个解决方案,但不适用于我的情况。 I want to use Play to built a http mock server in a test in Java. 我想使用Play在Java测试中构建一个http模拟服务器。

@BodyParser.Of(BodyParser.Raw.class) has no sense @ BodyParser.Of(BodyParser.Raw.class)没有意义

package org.dan;

import org.junit.Test;
import play.mvc.BodyParser;
import play.mvc.Result;
import play.server.Server;

//import static play.mvc.Controller.request;
import static play.mvc.Results.ok;
import static play.routing.RoutingDsl.fromComponents;
import static play.server.Server.forRouter;

import static play.mvc.Http.Context.Implicit.request;

public class DemoPlayTest {
    @Test
    public void run() throws InterruptedException {
        Server server = forRouter(
                9001,
                (components) ->
                        fromComponents(components)                                
                                .POST("/echo")
                                .routeTo(DemoPlayTest::action)
                                .build());
        Thread.sleep(1111111);
    }

    @BodyParser.Of(BodyParser.Raw.class)
    public static Result action() {
        return ok("Gut: " + request().body().asRaw() + "\n");
    }
}

Testing: 测试:

$ curl -v -X POST -d hello  http://localhost:9001/echo
Gut: null

Dependencies: 依赖关系:

     <play.version>2.6.17</play.version>
     <dependency>
        <groupId>com.typesafe.play</groupId>
        <artifactId>play-server_2.11</artifactId>
        <version>${play.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.typesafe.play</groupId>
        <artifactId>play-akka-http-server_2.11</artifactId>
        <version>${play.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.typesafe.play</groupId>
        <artifactId>play-java_2.11</artifactId>
        <version>${play.version}</version>
        <scope>test</scope>
    </dependency>
package org.dan;

import org.junit.Test;
import play.mvc.Result;
import play.server.Server;

import static play.mvc.Controller.request;
import static play.mvc.Results.ok;
import static play.routing.RoutingDsl.fromComponents;
import static play.server.Server.forRouter;

public class DemoPlayTest {    
    @Test
    public void run() throws InterruptedException {
        Server server = forRouter(
                9001,
                (components) ->
                        fromComponents(components)                                    
                                .POST("/echo")
                                .routeTo(DemoPlayTest::action)
                                .build());
        Thread.sleep(1111111);
    }

    public static Result action() {
        final String body = new String(request().body().asBytes().toArray());
        return ok("Gut: " + body + "\n");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果客户端设置 content-type="application/xml" 并且 body=null,如何解析 POST 请求的请求正文? - How to parse request body of POST request, if client set content-type="application/xml" and body=null? 如何发送请求标题是“内容类型”:“应用程序/ json”在排球时获取 - How to send request Header is “Content-Type”:“application/json” when GET on Volley 如何在 Spring Boot 中点击 Controller 组件之前操作 Content-Type 请求标头? - How to manipulate the Content-Type request header before hitting the Controller component in Spring Boot? 无法测试没有内容类型的 PUT 请求 - Unable to test PUT request without content-type Angular Spring Boot:重定向错误:在飞行前响应中Access-Control-Allow-Headers不允许请求标头字段Content-Type - Angular Spring Boot: Redirection error: Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response 取消 object 从 REST 请求与 Content-Type 到 application/xml - unmarchall object from REST request with Content-Type to application/xml 如何以编程方式(在TomEE中)获取SOAP请求的内容类型和编码? - How to obtain content-type and encoding of a SOAP request programmatically (in TomEE)? Java/Quarkus如何识别传入请求的Content-type - Java/Quarkus how to identify the Content-type of the incoming request 如何从SOAP请求的HTTP内容类型中删除操作参数? - How to remove the action parameter from HTTP Content-type of SOAP request? 无法在SOAP请求上设置Content-Type - Unable to set Content-Type on SOAP Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM