Work with Multipart Data

The following example iterates over a multipart payload and extracts data from each part.

The example uses the following functions:

  • read (in the script’s header) reads the multipart content from a multi variable that sets the boundary to 34b21.

  • mapObject iterates over the parts in the multipart data returned by read and returns the index and name key of each part within a JSON object.

DataWeave Script:
%dw 2.0
output application/json
var multi = '--34b21
Content-Disposition: form-data; name="text"
Content-Type: text/plain

Content-Disposition: form-data; name="file1"; filename="a.json"
Content-Type: application/json

  "title": "Java 8 in Action",
  "author": "Mario Fusco",
  "year": 2014
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html

<!DOCTYPE html>
  Available for download!
var parsed = read(multi, "multipart/form-data", {boundary:'34b21'})
parsed.parts mapObject ((value, key, index) -> {(index): key})
  "0": "text",
  "1": "file1",
  "2": "file2"

For additional examples, see Multipart Format (Form Data) examples and functions referenced in Multipart (dw::module::Multipart).

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Submit your feedback!
Share your thoughts to help us build the best documentation experience for you!
Take our latest survey!