【问题标题】:Multiple file upload with rest-assured多文件上传放心
【发布时间】:2017-01-28 11:42:07
【问题描述】:

我有一个 post api 调用,它允许我在名为“file”的表单数据参数中上传多个图像 在邮递员中,我可以将具有键“文件”和值作为多个文件的表单数据发送多个文件

我想用放心的 java 客户端模拟相同的调用。我可以放心上传单个文件但无法上传多个文件。

以下是单文件上传的放心代码:

File sheetFile = new File(sheetPath.get(0));
response = RestAssured.
          given().
              headers(headers).
              formParameter("studentDetail",
                      "{"+
                      "\"userId\" : "+PropFileHandler.readProperty("psUserId")+
                      ",\"institutionId\" : "+PropFileHandler.readProperty("institution_id")+
                      ",\"externalUserId\" : "+PropFileHandler.readProperty("user_id")+
                      ",\"tokenId\" : \"12000\""+
                      ",\"imagePathStatus\" : \"\""+
                      "}").
              formParameter("clientType", getData("ps_bulk_upload_image.clientType")).
              multiPart("file", sheetFile). #here i want to upload multile files
          when().
              post(relativePath).
          then().
              statusCode(200).
              body(matchesJsonSchema(new File(test.cngActions.getJsonSchemaDirectoryPath()+
                      getData("ps_bulk_upload_image.schemaPath")))).
              extract().response();

任何帮助将不胜感激。

【问题讨论】:

    标签: java postman rest-assured


    【解决方案1】:

    您只需要在需要的所有时间使用相同的 controlName 调用多部分方法。

    RestAssuredMockMvc
        .given()
            .log().all()
            .headers(httpHeaders)
            .contentType(MediaType.MULTIPART_FORM_DATA.toString())
            .multiPart("content", file1)
            .multiPart("content", file2)
    

    您发送以下请求

    Multiparts:     ------------
                Content-Disposition: form-data; name = content; filename = test1.txt
                Content-Type: application/octet-stream
    
                C:\Users\mgagomez\AppData\Local\Temp\junit7193983138466861544\test1.txt
                ------------
                Content-Disposition: form-data; name = content; filename = test2.txt
                Content-Type: application/octet-stream
    
                C:\Users\mgagomez\AppData\Local\Temp\junit7193983138466861544\test2.txt
    

    【讨论】:

      【解决方案2】:

      只需多次拨打multiPart

      请看这里:https://blog.jayway.com/2011/09/15/multipart-form-data-file-uploading-made-simple-with-rest-assured/

      相关代码:

      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");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        • 2011-09-08
        • 1970-01-01
        • 2013-01-27
        • 2012-01-21
        相关资源
        最近更新 更多