Contact Us 1-800-596-4880

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

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

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

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

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