2

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.

aled
  • 21,330
  • 3
  • 27
  • 34
Matt P
  • 147
  • 1
  • 6
  • What do you mean for "should I utilize application/json instead?"? – aled Nov 06 '22 at 20:08
  • Don't have the exact words for it, but rather than use multipart, to send the payload as a single payload and use a json object with key/value pairs, base64 encoding the files. i.e is there a better way to send files than multipart – Matt P Nov 06 '22 at 20:11
  • Well that's an alternative input but you would need to change the application. I believe REST APIs usually do that. Anyway, I suggested a solution. – aled Nov 06 '22 at 20:14

1 Answers1

3

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

aled
  • 21,330
  • 3
  • 27
  • 34