繁体   English   中英

如何通过 RestAssured 配置和发送多部分请求

[英]How to configure and send multipart request via RestAssured

我正在尝试配置和发送一个多部分请求,如下所示:

------boundary
Content-Disposition: form-data; name="before"; filename="blob"
Content-Type: application/vnd...+json;type=some_type

{some JSON}
------boundary
Content-Disposition: form-data; name="after"; filename="blob"
Content-Type: application/vnd...+json;type=some_type

{another JSON}
------boundary--

所以我尝试配置一个请求,如下面的代码

RestAssuredConfig config = RestAssured.config().multiPartConfig(
        new MultiPartConfig().defaultCharset(StandardCharsets.UTF_8).
                defaultBoundary("--boundary--"));
MultiPartSpecification m1 new MultiPartSpecBuilder(
        new ObjectMapper().writeValueAsString(some_JSON_transformed_to_HashMap)).
                fileName("blob").controlName("before").
                mimeType(ContentType.TEXT.getAcceptHeader()).
                header("ContentType", "application/vnd...+json;type=some_type").build();
MultiPartSpecification m2 = new MultiPartSpecBuilder(
        new ObjectMapper().writeValueAsString(another_JSON_transformed_to_HashMap)).
                fileName("blob").controlName("after").
                mimeType(ContentType.TEXT.getAcceptHeader()).
                header("ContentType", "application/vnd...+json;type=some_type").build();
RequestSpecification request = RestAssured.given().multiPart(m1).multiPart(m2).
        config(config).
          .header("Content-Type", "multipart/form-data; boundary=" + config.getMultiPartConfig().defaultBoundary());
request.post("some_url");

但是当我尝试执行它时,服务器说资源无效,但我相信 JSON 是正确的,所以我想我的多部分配置不正确。 我应该如何配置请求?

您可以尝试类似于下面的代码。

given().auth().preemptive()
                .basic("Jirausername", "Jirapassword")
                .header("X-Atlassian-Token", "nocheck")
                .multiPart(new File("/home/users/cat.log"))
                .when().post("http://localhost:8181/rest/api/2/issue/STS-223/attachments"); 

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM