This helper module provide functions for creating MultiPart and formats and parts (including fields and boundaries) of MultiPart formats.
To use this module, you must import it into your DataWeave code, for example,
by adding the line import dw::module::Multipart
to the header of your
DataWeave script.
Functions
field
field({| name: String, value: Any, mime?: String, fileName?: String |}): MultipartPart
Creates a MultipartPart
data structure using the specified part name,
input content for the part, format (or mime type), and optionally, file name.
This version of the field
function accepts arguments as an array of objects
that use the parameter names as keys, for example:
Multipart::field({name:"order",value: myOrder, mime: "application/json", fileName: "order.json"})
Parameters
Name | Description |
---|---|
|
Array of objects that specifies:
|
Example
This example produces two parts. The first part (named order
) outputs
content in JSON and provides a file name for the part (order.json
). The
second (named clients
) outputs content in XML and does not provide a file
name. Also notice that in this example you need to add the function’s
namespace to the function name, for example, Multipart::field
.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
%dw 2.0
import dw::module::Multipart
output multipart/form-data
var myOrder = [
{
order: 1,
amount: 2
},
{
order: 32,
amount: 1
}
]
var myClients = {
clients: {
client: {
id: 1,
name: "Mariano"
},
client: {
id: 2,
name: "Shoki"
}
}
}
---
{
parts: {
order: Multipart::field({name:"order",value: myOrder, mime: "application/json", fileName: "order.json"}),
clients: Multipart::field({name:"clients", value: myClients, mime: "application/xml"})
}
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
------=_Part_8032_681891620.1542560124825
Content-Type: application/json
Content-Disposition: form-data; name="order"; filename="order.json"
[
{
"order": 1,
"amount": 2
},
{
"order": 32,
"amount": 1
}
]
------=_Part_8032_681891620.1542560124825
Content-Type: application/xml
Content-Disposition: form-data; name="clients"
<clients>
<client>
<id>1</id>
<name>Mariano</name>
</client>
<client>
<id>2</id>
<name>Shoki</name>
</client>
</clients>
------=_Part_8032_681891620.1542560124825--
field(String, Any, String, String): MultipartPart
Creates a MultipartPart
data structure using the specified part name,
input content for the part, format (or mime type), and optionally, file name.
This version of the field
function accepts arguments in a comma-separated
list, for example:
1
Multipart::field("order", myOrder,"application/json", "order.json")`
Parameters
Name | Description |
---|---|
|
A set of parameters that specify:
|
Example
This example produces two parts. The first part (named order
) outputs
content in JSON and provides a file name for the part (order.json
). The
second (named clients
) outputs content in XML and does not provide a file
name. The only difference between this field
example and the previous
field
example is the way you pass in arguments to the method. Also notice
that in this example you need to add the function’s namespace to the function
name, for example, Multipart::field
.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
%dw 2.0
import dw::module::Multipart
output multipart/form-data
var myOrder = [
{
order: 1,
amount: 2
},
{
order: 32,
amount: 1
}
]
var myClients = {
clients: {
client: {
id: 1,
name: "Mariano"
},
client: {
id: 2,
name: "Shoki"
}
}
}
---
{
parts: {
order: Multipart::field("order", myOrder, "application/json", "order.json"),
clients: Multipart::field("clients", myClients, "application/xml")
}
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
------=_Part_4846_2022598837.1542560230901
Content-Type: application/json
Content-Disposition: form-data; name="order"; filename="order.json"
[
{
"order": 1,
"amount": 2
},
{
"order": 32,
"amount": 1
}
]
------=_Part_4846_2022598837.1542560230901
Content-Type: application/xml
Content-Disposition: form-data; name="clients"
<clients>
<client>
<id>1</id>
<name>Mariano</name>
</client>
<client>
<id>2</id>
<name>Shoki</name>
</client>
</clients>
------=_Part_4846_2022598837.1542560230901--
file
file({| name: String, path: String, mime?: String, fileName?: String |})
Creates a MultipartPart
data structure from a resource file.
This version of the file
function accepts arguments as an array of objects
that use the parameter names as keys, for example:
1
Multipart::file({ name: "myFile", path: "myClients.json", mime: "application/json", fileName: "partMyClients.json"})
Parameters
Name | Description |
---|---|
|
Array of objects that specifies:
|
Example
This example inserts file content from a MultipartPart
into a Multipart
data structure. It uses the form
function to create the Multipart
and uses file
to create a part named myClient
with JSON content from
an external file myClients.json
. It also specifies partMyClients.json
as
the value for to the filename
parameter.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import dw::module::Multipart
output multipart/form-data
var myClients = "myClients.json"
var myArgs = { name: "myFile", path: "myClients.json", mime: "application/json", * fileName: "partMyClients.json"}
---
Multipart::form([
Multipart::file(myArgs)
])
Input
A file called myClients.json
and located in src/main/resources
with the
following content.
1
2
3
4
5
6
7
8
9
10
clients: {
client: {
id: 1,
name: "Mariano"
},
client: {
id: 2,
name: "Shoki"
}
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
------=_Part_1586_1887987980.1542569342438
Content-Type: application/json
Content-Disposition: form-data; name="myFile"; filename="partMyClients.json"
{
clients: {
client: {
id: 1,
name: "Mariano"
},
client: {
id: 2,
name: "Shoki"
}
}
}
------=_Part_1586_1887987980.1542569342438--
file(String, String, String, String)
Creates a MultipartPart
data structure from a resource file.
This version of the file
function accepts arguments in a comma-separated
list, for example:
1
Multipart::field("myFile", myClients, 'application/json', "partMyClients.json")
Parameters
Name | Description |
---|---|
|
Array of objects that specifies:
|
Example
This example inserts file content from a MultipartPart
into a Multipart
data structure. It uses the form
function to create the Multipart
type
and uses file
to create a part named myClient
with JSON content from
an external file myClients.json
. It also specifies partMyClients.json
as
the value for to the filename
parameter.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import dw::module::Multipart
var myClients = "myClients.json"
output multipart/form-data
---
Multipart::form([
Multipart::file("myFile", myClients, 'application/json', "partMyClients.json")
])
Input
A file called myClients.json
and located in src/main/resources
with the
following content.
1
2
3
4
5
6
7
8
9
10
clients: {
client: {
id: 1,
name: "Mariano"
},
client: {
id: 2,
name: "Shoki"
}
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
------=_Part_1586_1887987980.1542569342438
Content-Type: application/json
Content-Disposition: form-data; name="myFile"; filename="partMyClients.json"
{
clients: {
client: {
id: 1,
name: "Mariano"
},
client: {
id: 2,
name: "Shoki"
}
}
}
------=_Part_1586_1887987980.1542569342438--
form
form(Array<MultipartPart>): Multipart
Creates a Multipart
data structure using a specified array of parts.
Parameters
Name | Description |
---|---|
|
An array of parts ( |
Example
This example creates a Multipart
data structure that contains parts, which
are described in examples for the field
function. For additional uses of
form
, see examples in the Multipart file
and field
documentation.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import dw::module::Multipart
output multipart/form-data
var firstPart = "content for my first part"
var secondPart = "content for my second part"
---
{
parts: {
part1: Multipart::field({name:"myFirstPart",value: firstPart}),
part2: Multipart::field("mySecondPart", secondPart)
}
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
------=_Part_320_1528378161.1542639222352
Content-Disposition: form-data; name="myFirstPart"
content for my first part
------=_Part_320_1528378161.1542639222352
Content-Disposition: form-data; name="mySecondPart"
content for my second part
------=_Part_320_1528378161.1542639222352--
=== generateBoundary
==== generateBoundary(Number): String
Helper function for generating boundaries in `Multipart` data structures.
== Types
=== Multipart
`MultiPart` type, a data structure for a complete `Multipart` format. See the
output example for the Multipart `form` function documentation.
.Definition
[source,DataWeave,linenums]
{ preamble?: String, parts: { _?: MultipartPart } }
=== MultipartPart `MultipartPart` type, a data structure for a part within a `MultiPart` format. See the output examples for the Multipart `field` function documentation. .Definition [source,DataWeave,linenums]
{ headers?: { "Content-Disposition"?: { name: String, filename?: String }, "Content-Type"?: String }, content: Any }