0

Currently I use the below code to post single file using RestAssured.

RestAssured.given().contentType(ContentType.MULTIPART_FORM_DATA.toString()).request().multiPart("files", ScreenshotFile).post().then().statusCode(200);

However I want to upload multiple files from the below mentioned FileList.

File ScreenShotFolder  = new File("C:\\Users\\1451615\\Desktop\\SessionScreenshot\\");
File ScreenShotFiles[] = ScreenShotFolder.listFiles();
jrtc27
  • 8,496
  • 3
  • 36
  • 68
  • [Solution Link](https://stackoverflow.com/a/63047470/14868118) Have you tried the solution on this link? – oktaykcr Mar 20 '21 at 10:54
  • `RequestSpecification request = RestAssured.given().contentType(ContentType.MULTIPART_FORM_DATA.toString()).request(); for (File file: ScreenShotFiles) { request.multiPart("files", new File(file.getAbsolutePath())); } request.post().then().statusCode(200);` – Jimmy Mar 21 '21 at 13:14

2 Answers2

1

I have put a for loop to post multiple files in the same request. Please find below the code for same.

File ScreenShotFolder = new File("C:\\Users\\1451615\\Desktop\\SessionScreenshot\\");
File ScreenShotFiles[] = ScreenShotFolder.listFiles();
RestAssured.baseURI = "http://10.141.188.112:7080/PIMSelfService/testing/uploadResultImg";
RequestSpecification request = RestAssured.given().contentType(ContentType.MULTIPART_FORM_DATA.toString()).request();
for (File file: ScreenShotFiles) {
  System.out.println("File name: " + file.getName());
  String FilePath = file.getAbsolutePath();
  File ScreenShotPath = new File(FilePath);
  System.out.println(ScreenShotPath);
  request.multiPart("files", ScreenShotPath);
}
request.post().then().statusCode(200);
oktaykcr
  • 346
  • 6
  • 12
0
ValidatableResponse createAttachemnetResponse = expect()
                .given()
                .spec(requestSpecification)
                .header("content-type", "multipart/form-data")
                .multiPart("files-0", new File("testImages/1.jpg"))
                .multiPart("files-1", new File("testImages/2.png"))
                .multiPart("files-2", new File("testImages/3.png"))
                .multiPart("files-3", new File("testImages/4.png"))
                .multiPart("files-4", new File("testImages/5.png"))
                .formParams("txn_id", transactionId)
                .when()
                .post(TRANSACTION_BASEPATH + POST_ATTACHMENT)
                .then()
                .spec(responseSpecification);