How to send a multipart request with RestAssured?

21.3k Views Asked by At

I have @Controller with method with signature like this:

@PostMapping
@ResponseBody
public ResponseEntity<Result> uploadFileAndReturnJson(@RequestParam("file") MultipartFile file) {}

I want to construct multipart request without physically creating any file. I tried doing it like this:

private MultiPartSpecification getMultiPart() {
    return new MultiPartSpecBuilder("111,222")
            .mimeType(MimeTypeUtils.MULTIPART_FORM_DATA.toString())
            .controlName("file")
            .fileName("file")
            .build();
}

Response response = RestAssured.given(this.spec)
            .auth().basic("admin", "admin")
            .multiPart(getMultiPart())
            .when().post(URL);

Unfortunately I received response:

Required request part 'file' is not present

I tried looking at RestAssured unit tests and it seems I'm doing it correctly. If I try to pass byte[] or InputStream instead of String, an exception is thrown:

Cannot retry request with a non-repeatable request entity.

Thanks for help.

3

There are 3 best solutions below

0
On BEST ANSWER

Your code looks fine and it should work with byte[]. You can use MultiPartSpecBuilder(byte[] content) like below.

private MultiPartSpecification getMultiPart() {
         return new MultiPartSpecBuilder("Test-Content-In-File".getBytes()).
                fileName("book.txt").
                controlName("file").
                mimeType("text/plain").
                build();
   }

The details for error you are getting with byte[] is available at https://github.com/rest-assured/rest-assured/issues/507. According to this you should try with preemptive basic auth like below.

.auth().preemptive.basic("admin", "admin")
0
On

I needed to send multiple request with files and json data , i solve it like that

public static Response Post(JSONObject body, String URL, String file1, String file2) {
        try {
            return RestAssured.given().baseUri(URL).urlEncodingEnabled(false)
                    .accept("application/json, text/plain, */*")
                    .multiPart("data",body,"application/json")
                    .multiPart("file[0]", new File(file1),"multipart/form-data")
                    .multiPart("file[1]", new File(file2),"multipart/form-data")
                    .relaxedHTTPSValidation().when().post();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }
0
On
try {

    RestAssured.given()
            .header(new Header("content-type", "multipart/form-data"))
            .multiPart("file",new File( "./src/main/resources/test.txt"))
            .formParam("description", "This is my doc")
            .auth().preemptive().basic(loginModel.getUsername(), loginModel.getPassword())
            .when()
            .post(URL)
            .then()
            .assertThat()
            .body(matchesJsonSchemaInClasspath("schemas/members/member-document.json"));
}
catch(Exception e) {
    Assert.assertEquals(false, true);
    logger.error(e.getMessage(), e);
}