Q15 of 40 · REST Assured

How do you handle file uploads in REST Assured (multipart)?

REST AssuredMidrest-assuredfile-uploadmultipartapi-testing

Short answer

Short answer: Use .multiPart("file", new File(path)) in the given() block — REST Assured sets Content-Type: multipart/form-data automatically. Add .multiPart("fieldName", value) calls for other form fields. Set content type explicitly with .multiPart("file", file, "text/csv") if the server is type-sensitive.

Detail

REST Assured maps .multiPart() calls to a multipart/form-data request body. Each call adds one part:

given()
    .multiPart("file",        new File("src/test/resources/data.csv"), "text/csv")
    .multiPart("description", "Monthly import")
    .multiPart("ownerId",     "42")
.when()
    .post("/imports")
.then()
    .statusCode(202);

REST Assured sets the Content-Type: multipart/form-data; boundary=... header automatically — do not set it manually, as you'd override the generated boundary.

Binary content from classpath:

given()
    .multiPart("image", "avatar.png",
        getClass().getResourceAsStream("/images/avatar.png"), "image/png")

Assertion after upload: verify the server processed the file by asserting on the response body — e.g., the imported row count, the stored file URL, or a job ID for async processing.

// EXAMPLE

@Test
void uploadCsv_returnsJobId() {
    File csvFile = new File("src/test/resources/users-import.csv");

    String jobId = given(reqSpec)
        .multiPart("file",        csvFile, "text/csv")
        .multiPart("batchSize",   "100")
        .multiPart("validateOnly", "false")
        // Don't set Content-Type here — REST Assured adds it with the boundary
    .when()
        .post("/imports/users")
    .then()
        .statusCode(202)
        .body("status", equalTo("QUEUED"))
        .extract().path("jobId");

    assertThat(jobId).isNotBlank();
}

// WHAT INTERVIEWERS LOOK FOR

Correct use of .multiPart(), knowing that REST Assured auto-sets Content-Type (don't override it), and awareness of the overload that takes an InputStream for classpath resources. Asserting on the processing result, not just the status code, signals test quality.

// COMMON PITFALL

Setting .contentType("multipart/form-data") manually and overwriting the auto-generated boundary, which causes the server to fail to parse the request body.