Search code examples
mulemultipartform-datadataweavemulesoftmule4

Accessing multiple files in a multipart request


Most of the documentation in Mule regarding multipart relates to making a request and not processing an incoming request.

I'm looking to receive multiple files in an incoming request as an array and then loop through them to upload to another service (Salesforce).

Due to the use case I'm sending two properties:

  • data which is metadata to create a case record
  • files which is an array of files to be associated with the case record

My Postman request is: enter image description here

This is the payload I receive in a Mule 4 application (redacted the file data itself):

----------------------------778704367722595657997650
Content-Disposition: form-data; name="data"
Content-Type: application/json

[
    {    
        "subject": "test
    }
]
----------------------------778704367722595657997650
Content-Disposition: form-data; name="files"; filename="logo.jpeg"
Content-Type: image/jpeg

jpegdata
----------------------------778704367722595657997650
Content-Disposition: form-data; name="files"; filename="icon.png"
Content-Type: image/png

pngdata
----------------------------778704367722595657997650--

I attempt to get the files with Dataweave: payload.parts.files

But this returns only the first file 'logo.jpeg'

I've attempted to send the files as files[] but it doesn't seem to send as an array.

It seems that I should loop through the boundary instead? If so, should my request set the boundary so it's the same each time?

Or is there a different way to access the files? Secondarily, is multipart the correct method or should I utilize application/json instead?

The use case is to send files from a contact form in another application to Salesforce.


Solution

  • When DataWeave parses a multi part input it outputs an object in which each key is the name of the part. Your input has two entries with the same key name. As usual in DataWeave you should use the multi valued selector to get all the values with the same key: payload.parts.*files