</>

Technology

Rest Assured

Difficulty

Intermediate

Interview Question

How to handle multipart file upload in Rest Assured?

Answer

Multipart File Upload in Rest Assured

File uploads use multipart/form-data content type. Rest Assured supports this via the .multiPart() method.

Method 1: Upload a File from Disk

Java
@Test
public void testFileUpload() {

    File uploadFile = new File("src/test/resources/testfile.pdf");

    Response response = given()
                            .header("Authorization", "Bearer " + token)
                            .multiPart("file", uploadFile, "application/pdf")
                            .when()
                            .post("/api/files/upload");

    assertEquals(response.getStatusCode(), 200);
    assertNotNull(response.jsonPath().getString("file_id"));
    assertEquals(response.jsonPath().getString("filename"), "testfile.pdf");
}

Method 2: Upload with Additional Form Fields

Java
@Test
public void testFileUploadWithMetadata() {

    File imageFile = new File("src/test/resources/profile.jpg");

    given()
        .header("Authorization", "Bearer " + token)
        .multiPart("file", imageFile, "image/jpeg")
        .multiPart("title", "Profile Picture")
        .multiPart("description", "User avatar image")
        .multiPart("category", "profile")
        .when()
        .post("/api/media/upload")
        .then()
        .statusCode(201)
        .body("url", containsString("https://"))
        .body("type", equalTo("image/jpeg"));
}

Method 3: Upload Multiple Files

Java
@Test
public void testMultipleFileUpload() {

    File file1 = new File("src/test/resources/doc1.pdf");
    File file2 = new File("src/test/resources/doc2.pdf");

    given()
        .header("Authorization", "Bearer " + token)
        .multiPart("files", file1, "application/pdf")
        .multiPart("files", file2, "application/pdf")
        .when()
        .post("/api/files/batch-upload")
        .then()
        .statusCode(200)
        .body("uploaded_count", equalTo(2));
}

Method 4: Upload File Content as Byte Array

Java
@Test
public void testUploadFromByteArray() throws IOException {

    byte[] fileContent = Files.readAllBytes(
        Paths.get("src/test/resources/testfile.csv")
    );

    given()
        .header("Authorization", "Bearer " + token)
        .multiPart("file", "data.csv", fileContent, "text/csv")
        .when()
        .post("/api/import/csv")
        .then()
        .statusCode(200)
        .body("imported_rows", greaterThan(0));
}

Method 5: Validate Upload Constraints

Java
@Test
public void testUploadTooLargeFile() {

    File largeFile = new File("src/test/resources/large_50mb.zip");

    given()
        .header("Authorization", "Bearer " + token)
        .multiPart("file", largeFile, "application/zip")
        .when()
        .post("/api/files/upload")
        .then()
        .statusCode(413) // 413 Payload Too Large
        .body("error", containsString("File size exceeds limit"));
}

@Test
public void testUploadInvalidFileType() {

    File exeFile = new File("src/test/resources/malware.exe");

    given()
        .header("Authorization", "Bearer " + token)
        .multiPart("file", exeFile, "application/octet-stream")
        .when()
        .post("/api/files/upload")
        .then()
        .statusCode(400)
        .body("error", containsString("File type not allowed"));
}

Content Type Note

Rest Assured automatically sets Content-Type: multipart/form-data when you use .multiPart() — you do NOT need to set it manually.

Follow AutomateQA

Related Topics