问题描述
我需要使用Java DSL REST-assured上传多个文件,以便轻松测试REST服务。我成功上传了一个文件。但是我无法上传多文件。有人可以帮我在一个请求中上传多个文件吗?
RestAssured.given().auth().oauth2(acesstoken)
.multiPart("file",new File("temp.pdf"),"application/pdf")
.when().post("https://www.example.com").then().log().all();
解决方法
根据docs
也可以在同一请求中提供多个“多部分”实体
通过连续链接多个multiPart()方法调用:
RestAssured.given().auth().oauth2(acessToken)
.multiPart("file",new File("temp.pdf"),"application/pdf")
.multiPart("file",new File("readme.txt"),"text/plain")
.when().post("https://www.example.com").then().log().all();
,
通过以下链接How to pass multiple files as a input to an api using Rest Assured,我们能够成功上传多个文件 How to pass multiple files as a input to an api using Rest Assured
公共静态void main(String [] args)抛出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());
}