如何使用 Rest Assured 将多个文件作为输入传递给 api
How to pass multiple files as a input to an api using Rest Assured
在下面的这个方法中,我正在对本地 运行ning API 进行 API 调用,它只接受一个文件作为请求,但是有没有任何可能的方法一个 API 调用,它根据 API 的请求在 运行 时间使用 Rest Assured 动态接受多个文件作为请求?比如如何在 运行 时间动态地将多个文件作为 API 请求添加到 Rest Assured 中?
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();
}
请放心使用构建器模式,因此您可以像
一样堆叠文件
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");
因此您可以发送多个文件。
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());
}
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);
在下面的这个方法中,我正在对本地 运行ning API 进行 API 调用,它只接受一个文件作为请求,但是有没有任何可能的方法一个 API 调用,它根据 API 的请求在 运行 时间使用 Rest Assured 动态接受多个文件作为请求?比如如何在 运行 时间动态地将多个文件作为 API 请求添加到 Rest Assured 中?
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();
}
请放心使用构建器模式,因此您可以像
一样堆叠文件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");
因此您可以发送多个文件。
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());
}
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);