简体   繁体   English

如何使用 Rest 将多个文件作为输入传递给 api

[英]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.

相关问题 更新来自 API 的 JSON 响应,并使用 Rest Assured 将更新后的响应作为输入/正文传递给另一个 API - Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured 如何从get api获取csrf令牌并使用REST Assured将csrf令牌传递给另一个post api? - How to get csrf token from get api and pass csrf token to another post api using REST Assured? 如何使用Rest Assured将数组列表添加到Rest API请求? - How to add array list to rest API request using Rest Assured? 如何使用放心测试需要身份验证的 Rest API - How to test Rest API which require authentication using Rest assured 如何使用 REST assured 将 API 响应中的值存储为全局变量并将其作为 Cucumber 功能文件中的参数传递给另一个 API - How can i store a value as global variable from an API response and pass it to another API as parameter in Cucumber feature file using REST assured 如何使用 Rest-assured 在 api 中发送表单数据 - How to send form-data in api using Rest-assured 如何使用 Rest Assured 在 API 请求中添加 Bearer 令牌 - How to add Bearer token in API rerquest using Rest Assured 如何在放心的标头中传递授权令牌? - How to pass authorization token in header in Rest assured? 如何将参数传递给 Rest-Assured - How to pass parameters to Rest-Assured 如何在 rest 中通过 Headers 在 java 中保证测试 - How to pass Headers in rest assured test in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM