Pradeep maiya
Pradeep maiya

Reputation: 27

How to pass multiple files as a input to an api using Rest Assured

In this method below I was making an API call to a locally running API that accepts only one file as a request, but is there any possible way to make an API call that accepts multiple files as request using Rest Assured dynamically at run time based on requests for that API? like how to add multiple files as an API request in Rest Assured at run time dynamically?

    public String restTest() {
        String resp = RestAssured.given().multiPart("file", new File("C:/Local/file/path/LocalFiles/file.txt")).when().post("http://localhost:4444/local/upload").then().assertThat().statusCode(200).and().extract().body().asString();

    return resp.toString();
        
    }

Upvotes: 1

Views: 2805

Answers (3)

Jimmy
Jimmy

Reputation: 1051

File files[] = getFileList();
RequestSpecification request = RestAssured.given().contentType(ContentType.MULTIPART_FORM_DATA.toString()).request();
for (File file: files) {
  request.multiPart("files", new File(file.getAbsolutePath()));
}
request.post().then().statusCode(200);

Upvotes: 0

MonicaA
MonicaA

Reputation: 66

public static void main(String[] args) throws MalformedURLException {

        Response response;
        RequestSpecification request = RestAssured.given().header("content-type", "multipart/form-data");
        for (int i = 1; i <= 2; i++) {
            request.multiPart("file", new File("D:/testtemplates98_" + i + "Data.xlsx"));// File parameters will be
                                                                                            // dynamic
        }
        response = request.post(new URL("https://jquery-file-upload.appspot.com/"));
        System.out.println(response.getBody().asString());
}

Upvotes: 2

Tobsch
Tobsch

Reputation: 175

rest assured uses a builder pattern so you can just stack the files like

given().
         multiPart("file1", new File("/home/johan/some_large_file.bin")).
         multiPart("file2", new File("/home/johan/some_other_large_file.bin")).
         multiPart("file3", "file_name.bin", inputStream).
         formParam("name", "value").
expect().
         body("fileUploadResult", is("OK")).
when().
         post("/advancedFileUpload");

and so you can send multiple files.

Upvotes: 0

Related Questions