简体   繁体   中英

How do I upload multiple files with multipart in Karate

I would like to upload multiple image files in one request using multipart. I have reviewed the Karate examples on this, but the multiple file upload does not meet my need (/multiple endpoint here - https://github.com/intuit/karate/blob/master/karate-demo/src/main/java/com/intuit/karate/demo/controller/UploadController.java ). My service method (Spring REST) signature expects an array of MultipartFile[] so that I can accept any number of files. Here is my scenario:

Scenario: Upload multiple files
    * def json = {}
    * set json.files[0] = { read: 'file1.jpg', filename: 'file1.jpg', contentType: 'image/jpeg' }
    * set json.files[1] = { read: 'file2.jpg', filename: 'file2.jpg', contentType: 'image/jpeg' }
    Given path '/rest' 
    And multipart files json
    When method post
    Then status 200

And here is the Spring web service method (just trying to receive the files right now, so the method doesn't do much):

@PostMapping("/rest")
public String handleFileUpload(@RequestParam("file") MultipartFile[] file) {
   System.out.println("Len: " + file.length);
   for(MultipartFile currentFile : file) {
      System.out.println("In here: " + currentFile.getOriginalFilename());
   }
   return file[0].getOriginalFilename();
}

When I run this I receive a Karate error: 'multipart file value should be json'

If I change the scenario to do this:

    Scenario: Upload multiple files
        * def json = {}
        * set json.files = { read: 'file1.jpg', filename: 'file1.jpg', contentType: 'image/jpeg' }, { read: 'file2.jpg', filename: 'file2.jpg', contentType: 'image/jpeg' }
        Given path '/rest' 
        And multipart files json
        When method post
        Then status 200

Then the test executes ok, but only one file ends up in the MultipartFile array 'files' (service method argument).

What is the proper way to upload multiple files to the web service method above using Karate?

Edit: Adding client code (below) and updated Spring method above.

Here is a simple HTML form that will submit multiple files to the Spring method above:

<form method="POST" enctype="multipart/form-data" action="/rest">
    <table>
        <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
        <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
        <tr><td></td><td><input type="submit" value="Upload" /></td></tr>
    </table>
</form>

When submitted I get 2 files in the service method.

Seems that it does work, you just need to send the same parameter twice in the feature file

And multipart file files = { read: 'mergeTest.pdf', filename: 'upload-name.pdf', contentType: 'application/pdf' }

And multipart file files = { read: 'mergeTest.pdf', filename: 'upload-name.pdf', contentType: 'application/pdf' }

And multipart field filesMetadata = .........................................

my service:

public ResponseEntity<StreamingResponseBody> mergePdfs(@RequestPart @Validated
            List<FileMetaData> filesMetadata, @RequestPart @Validated List<MultipartFile> files)

Wow, never seen this before and it is likely that Karate does not support it. I'm also wondering if this is legal as per the HTTP spec - as far as I know - each file has to have unique field-name. Do you have the corresponding client-side code for the Apache HTTP Client, that would help.

EDIT: also see answer by Esteban Lopez below: https://stackoverflow.com/a/59833358/143475

Your best bet may be to submit a feature request and also contribute code to expedite. Note that this is the first time in 2 years that anyone has reported this as a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM