Reputation: 27
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
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
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
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