1

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();
        
    }
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26

3 Answers3

2
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());
}
Vivek Jain
  • 2,730
  • 6
  • 12
  • 27
MonicaA
  • 66
  • 1
  • 4
0

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.

Tobsch
  • 175
  • 2
  • 10
0
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);
Jimmy
  • 995
  • 9
  • 18