[英]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? 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 API 的请求? like how to add multiple files as an API request in Rest Assured at run time dynamically?就像如何在 Rest 中动态添加多个文件作为 API 请求?
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();
}
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());
}
rest assured uses a builder pattern so you can just stack the files like rest 保证使用构建器模式,因此您可以像这样堆叠文件
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.所以你可以发送多个文件。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.