简体   繁体   中英

How to configure and send multipart request via RestAssured

I'm trying to configure and send a multipart request like the following one:

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

So I tried to configure a request, as in the code below

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");

But when I try to execute it, server says that resource is invalid, but I believe, that JSONs is correct, so I suppose my multipart configuration is incorrect. How should I configure a request?

You could try something similar to below code.

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"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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