---
schema_version: "1"
id: 762c77a018a6b43e6bcf9cbe795accd166e0f1d1a0789fd88c29be4f191740c4
title: Multipart (Form Data) Format
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dataweave-formats-multipart.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dataweave-formats-multipart
source_site: docs.mulesoft.com
content_type: documentation
content_hash: f2198016d07456bd6caa474ac5fefb9f017aa5644f1c4431da304add521805dc
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
keywords: format, multipart, multipart/*, multipart/form-data
---

# Multipart (Form Data) Format

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

MIME type: `multipart/form-data`

ID: `multipart`

DataWeave supports Multipart subtypes, in particular `form-data`. These formats enable you to handle
several different data parts in a single payload, regardless of the format each part has. To
distinguish the beginning and end of a part, a boundary is used and metadata for each part can be
added through headers.

DataWeave represents a Multipart document with the given `Object` structure:

```weave
type Multipart = {
  preamble?: String,
  parts: {
    _?: MultipartPart
  }
}
type MultipartPart = {
  headers?: {
    "Content-Disposition"?: {
      name: String,
      filename?: String
    },
    "Content-Type"?: String
  },
  content: Any
}
```

## Examples

The following examples show uses of the Multipart format.

- [Example: Multipart](#example1)

- [Example: Access and Transform Data from Parts](#example2)

- [Example: Generate Multipart Content](#example3)

- [Example: Iterate through Multipart payload](#example4)

- [Example: Using ^mediaType and ^mimeType Metadata Selectors](#example5)

### Example: Multipart

This example shows how DataWeave reads simple Multipart content.

#### Input

The Multipart input serves as the payload to the DataWeave source.

```text
--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

<title> Available for download! </title>
--34b21--
```

#### Source

The DataWeave script transforms the Multipart input payload to the DataWeave (dw) format.

```weave
%dw 2.0
output application/dw
---
payload
```

#### Output

The output shows how the DataWeave (dw) format represents the Multipart input. Note that the `raw`
and `content` values are shortened for brevity. The full values are longer.

```weave
{
    parts: {
        text: {
            headers: {
                "Content-Disposition": {
                    name: "text",
                    subtype: "form-data"
                },
                "Content-Type": "text/plain"
            },
        content: "Book" as String {
            raw: "Qm9vaw==" as Binary {
                    base: "64"
                },
                encoding: null,
                mediaType: "text/plain",
                mimeType: "text/plain"
            }
        },
        file1: {
            headers: {
                "Content-Disposition": {
                    name: "file1",
                    filename: "a.json",
                    subtype: "form-data"
                },
                "Content-Type": "application/json"
            },
            content: {
                title: "Java 8 in Action",
                author: "Mario Fusco",
                year: 2014
            } as Object {
                raw: "ewogICJ0aXRsZSI6ICJKYXZhI...==" as Binary {
                    base: "64"
                },
                encoding: null,
                mediaType: "application/json",
                mimeType: "application/json"
            }
        },
        file2: {
            headers: {
                "Content-Disposition": {
                    name: "file2",
                    filename: "a.html",
                    subtype: "form-data"
                },
                "Content-Type": "text/html"
            },
            content: "PCFET0NUWVBFIGh0bWw+Cjx0aXRsZT4KI...==" as Binary {
                base: "64"
            }
       }
    }
}
----
```

### Example: Access and Transform Data from Parts

Within a DataWeave script, you can access and transform data from any of the parts by selecting the
`parts` element. Navigation can be array-based or key-based when parts feature a name to reference
them by. The part’s data can be accessed through the `content` keyword, while headers can be
accessed through the `headers` keyword.

#### Input

This example serves as input to separate DataWeave scripts. It shows a raw `multipart/form-data`
payload with a `34b21` boundary consisting of 3 parts:

- a `text/plain` one named `text`

- an `application/json` file (`a.json`) named `file1`

- a `text/html` file (`a.html`) named `file2`

Raw Multipart Data:

```text
--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--
```

#### Source

The following DataWeave script uses the raw `multipart/form-data` payload as input to produce
`Book:a.json`.

Reading Multipart Content:

```dataweave
%dw 2.0
output text/plain
---
payload.parts.text.content ++ ':' ++ payload.parts[1].headers.'Content-Disposition'.filename
```

### Example: Generate Multipart Content

You can generate multipart content that DataWeave uses to build an object with a list of parts, each
containing its headers and content. The following DataWeave script produces the raw multipart data
(previously analyzed) if the HTML data is available in the payload.

> [!NOTE] To provide a consistent output in the DataWeave documentation, the example hardcodes the
> `boundary` property. However, you do not need to hardcode this property in most cases because
> DataWeave generates a random boundary and adds it to the output MIME type.

Writing Multipart Content:

```dataweave
%dw 2.0
output multipart/form-data boundary='34b21'
---
{
  parts : {
    text : {
      headers : {
        "Content-Type": "text/plain"
      },
      content : "Book"
    },
    file1 : {
      headers : {
        "Content-Disposition" : {
            "name": "file1",
            "filename": "a.json"
        },
        "Content-Type" : "application/json"
      },
      content : {
        title: "Java 8 in Action",
        author: "Mario Fusco",
        year: 2014
      }
    },
    file2 : {
      headers : {
        "Content-Disposition" : {
            "filename": "a.html"
        },
        "Content-Type" : payload.^mimeType
      },
      content : payload
    }
  }
}
```

Notice that the key determines the part’s name if the name is not explicitly provided in the
`Content-Disposition` header, and note that DataWeave can handle content from supported formats, as
well as references to unsupported ones, such as HTML.

The following example shows how to send the current payload as a file part in a multipart form-data.
You need to use a DataWeave script similar to the following:

```dataweave
%dw 2.0
input payload application/xml
output multipart/form-data boundary="----myboundary----"
---
{
  parts: {
    filePart: {
      headers: {
        "Content-Disposition": {
          "name": "<NAME>",
          "filename": "<FILE_NAME>.<FILE_EXTENSION>"
        },
        "Content-Type": "<FILE_CONTENT-TYPE>"
      },
      content: payload
    },
    otherJsonPart: {
      content: '{"name": "sampleFile.xml", "sampleFile": {"type": "sample"} }'
    }
  }
}
```

- `boundary` sets the boundary value, for example `"----myboundary----"`.

- `<NAME>` is the tag name of your file, for example `"sampleFile"`.

- `<FILE_NAME>.<FILE_EXTENSION>` is the filename to populate the payload content, for example
  `"sampleFile.xml"`.

- `<FILE_CONTENT-TYPE>` matches the content of the file you send, for example `"application/xml"`.

- The content of the file is the current payload.

This example contains two parts in the multipart which are the file and the `otherJsonPart`. In case
you want to add more parts, add them under the `parts:{}` section with the following format:

```dataweave
  <PART_NAME>: {
    content: <PART_CONTENT>
  }
```

- `<PART_NAME>` is the name of the part without quotes, for example `json`.

- `<PART_CONTENT>` is the content for that part, for example
  `'{"name": "sampleFile.xml", "sampleFile": {"type": "sample"} }'`.

Based on the following XML input:

```xml
<Customers>
	<Customer>
		<Number>1</Number>
		<FirstName>Fred</FirstName>
		<LastName>Landis</LastName>
		<Address>
			<Street>Oakstreet</Street>
			<City>Boston</City>
			<ZIP>23320</ZIP>
			<State>MA</State>
		</Address>
	</Customer>
	<Customer>
		<Number>2</Number>
		<FirstName>Michelle</FirstName>
		<LastName>Butler</LastName>
		<Address>
			<Street>First Avenue</Street>
			<City>San-Francisco</City>
			<ZIP>44324</ZIP>
			<State>CA</State>
		</Address>
	</Customer>
	<Customer>
		<Number>3</Number>
		<FirstName>Ted</FirstName>
		<LastName>Little</LastName>
		<Address>
			<Street>Long Way</Street>
			<City>Los-Angeles</City>
			<ZIP>34424</ZIP>
			<State>CA</State>
		</Address>
	</Customer>
</Customers>
```

The DataWeave script is:

```dataweave
%dw 2.0
output multipart/form-data  boundary="----myboundary----"
---
{
  parts: {
    filePart: {
      headers: {
        "Content-Disposition": {
          "name": "test",
          "filename": "sampleFile.xml"
        },
        "Content-Type": "application/xml"
      },
      content: payload
    },
    otherJsonPart: {
      content: '{"name": "sampleFile.xml", "sampleFile": {"type": "sample"} }'
    }
  }
}
```

The output multipart is:

```multipart
------myboundary----
Content-Type: application/xml
Content-Disposition: form-data; name="test"; filename="sampleFile.xml"

<Customers>
  <Customer>
    <Number>1</Number>
    <FirstName>Fred</FirstName>
    <LastName>Landis</LastName>
    <Address>
      <Street>Oakstreet</Street>
      <City>Boston</City>
      <ZIP>23320</ZIP>
      <State>MA</State>
    </Address>
  </Customer>
  <Customer>
    <Number>2</Number>
    <FirstName>Michelle</FirstName>
    <LastName>Butler</LastName>
    <Address>
      <Street>First Avenue</Street>
      <City>San-Francisco</City>
      <ZIP>44324</ZIP>
      <State>CA</State>
    </Address>
  </Customer>
  <Customer>
    <Number>3</Number>
    <FirstName>Ted</FirstName>
    <LastName>Little</LastName>
    <Address>
      <Street>Long Way</Street>
      <City>Los-Angeles</City>
      <ZIP>34424</ZIP>
      <State>CA</State>
    </Address>
  </Customer>
</Customers>
------myboundary----
Content-Disposition: form-data; name="otherJsonPart"

{"name": "sampleFile.xml", "sampleFile": {"type": "sample"} }
------myboundary------
```

### Example: Iterate through Multipart payload

This example shows how DataWeave iterates through all the parts of the payload and processes the
each part individually.

### Source

```dataweave
%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

```json
{
  "0": "text",
  "1": "file1",
  "2": "file2"
}
```

### Example: Using ^mediaType and ^mimeType Metadata Selectors

The `^mimeType` selector returns the MIME type of a value without parameters, for example,
`application/json`. The `^mediaType` selector, by contrast, provides the MIME type and any other
parameters related to it, for example, `application/json;charset=UTF-16`.

The following example shows how to transform multipart inputs into multipart outputs with a
different format. The DataWeave script uses both the `^mimeType` and `^mediaType` selectors to
generate the new multipart content.

### Input

The multipart input has more than one Content-Type. In this case `application/json` and
`application/csv` including a non-standard separator:

```text
----------------------------049709565842371278701691
Content-Type: application/json
Content-Disposition: form-data; name="order"

{
"name": "order1",
"id": 123,
"content": "orderContent"
}
----------------------------049709565842371278701691
Content-Type: application/csv;separator=.
Content-Disposition: form-data; name="partner"

partnerName.id
mulesoft.1
----------------------------049709565842371278701691--
```

### Source

The DataWeave script transforms the multipart input into multipart output by defining
`output multipart/form-data` MIME type. The multipart output data contains two parts:

- `orderAck`

Contains a text with an acknowledgement of the receipt of an order. The Content-Type is
`text/plain`. The ^mimeType\` selector becomes useful to log the Content-Type as part of the
message.

- `partner`

Contains the original partner itself. Notice that this time, the Content-Type is
`payload.parts..^mediaType`. The ^mediaType selector includes also the separator, that parses the
`application/csv` content.

```dataweave
%dw 2.0
input payload multipart boundary='boundary1'
output multipart boundary='test2'
---
{
  parts : {
    orderAck : {
      headers : {
        "Content-Type": "text/plain"
      },
      content : "Order payload of type " ++ payload.parts.order.content.^mimeType ++ " and id " ++ (payload.parts.order.content.id as String) ++ " received. Receipt available."
    },
    partner: {
      headers : {
        "Content-Type": payload.parts.partner.content.^mediaType
      },
      content : payload.parts.partner.content
    }
  }
}
```

### Output

```text
--test2
Content-Type: text/plain
Content-Disposition: form-data; name="orderAck"

Order payload of type application/json and id 123 received. Receipt available.
--test2
Content-Type: application/csv;separator=.
Content-Disposition: form-data; name="partner"

partnerName.id
mulesoft.1
--test2--
```

## Configuration Properties

DataWeave supports the following configuration properties for this format.

### Reader Properties

This format accepts properties that provide instructions for reading input data.

<table><colgroup><col> <col> <col> <col></colgroup><thead><tr><th>Parameter</th><th>Type</th><th>Default</th><th>Description</th></tr></thead><tbody><tr><td><p><code>boundary</code></p></td><td><p><code>String</code></p></td><td><p><code>null</code></p></td><td><div><div><p>The multipart <code>boundary</code> value, a string to delimit parts.</p></div></div></td></tr><tr><td><p><code>defaultContentType</code></p></td><td><p><code>String</code></p></td><td><p><code>'application/octet-stream'</code></p></td><td><div><div><p>Sets the default Content-Type to use on parts of the <code>multipart/*</code> format. When set, this property takes precedence over the setting for the <a href="https://docs.mulesoft.com/dataweave/2.9/dataweave-system-properties">system property</a> <code>com.mulesoft.dw.multipart.defaultContentType</code>.</p></div><div><p><em>Introduced in DataWeave 2.3 (2.3.0-20210720) for the August 2021 release of Mule 4.3.0-20210719.</em></p></div></div></td></tr></tbody></table>

### Writer Properties

This format accepts properties that provide instructions for writing output data.

<table><colgroup><col> <col> <col> <col></colgroup><thead><tr><th>Parameter</th><th>Type</th><th>Default</th><th>Description</th></tr></thead><tbody><tr><td><p><code>boundary</code></p></td><td><p><code>String</code></p></td><td><p><code>null</code></p></td><td><div><div><p>The multipart <code>boundary</code> value, a string to delimit parts.</p></div></div></td></tr><tr><td><p><code>bufferSize</code></p></td><td><p><code>Number</code></p></td><td><p><code>8192</code></p></td><td><div><div><p>Size of the buffer writer, in bytes. The value must be greater than <code>8</code>.</p></div></div></td></tr><tr><td><p><code>deferred</code></p></td><td><p><code>Boolean</code></p></td><td><p><code>false</code></p></td><td><div><div><p>Generates the output as a data stream when set to <code>true</code>, and defers the script’s execution until the generated content is consumed.</p></div><div><p>Valid values are <code>true</code> or <code>false</code>.</p></div></div></td></tr></tbody></table>

## Supported MIME Types

This format supports the following MIME types.

<table><colgroup><col></colgroup><thead><tr><th>MIME Type</th></tr></thead><tbody><tr><td><p><code>multipart/*</code></p></td></tr></tbody></table>
