【问题标题】:Uploading Mutliple file using REST-assured [duplicate]使用 REST-assured 上传多个文件 [重复]
【发布时间】:2020-08-26 11:56:21
【问题描述】:

我需要使用REST-assured 上传多个文件,这是一种用于轻松测试 REST 服务的 Java DSL。我成功上传了一个文件。但我无法上传多个文件。有人可以帮我在一个请求中上传多个文件吗?

单个文件上传示例:

RestAssured.given().auth().oauth2(acessToken)
  .multiPart("file",new File("temp.pdf"),"application/pdf")
  .when().post("https://www.example.com").then().log().all();

【问题讨论】:

标签: java dsl rest-assured rest


【解决方案1】:

根据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();

【讨论】:

  • 非常感谢您的回复,我按照下面的 URL 帮助我上传了 n 个文件
【解决方案2】:

按照以下链接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

public static 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());

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 2020-06-10
    • 2019-01-17
    • 2023-03-20
    • 2020-11-09
    • 2017-02-28
    • 2015-12-04
    • 2013-12-09
    相关资源
    最近更新 更多