Multipart::file({ name: "myFile", path: "myClients.json", mime: "application/json", fileName: "partMyClients.json"})
file
file(opts: {| name: String, path: String, mime?: String, fileName?: String |})
Creates a MultipartPart
data structure from a resource file.
This version of the file
function accepts as argument an object containing key/value pairs, enabling you to enter the key/value pairs in any order, for example:
Parameters
Name | Description |
---|---|
|
An object that specifies the following key/value pairs:
|
Example
This example creates a MultipartPart
from a file accessible to the DataWeave function, the file name is orders.xml
and is located in the Mule application’s /src/main/resources
folder.
The file
function locates the external orders.xml
file and uses key/value pairs to indicate the various parameters needed to build the MultipartPart
.
-
The
name
can be anything, but it usually coincides with the required parameter needed by the receiving server that accepts thisMultipart
payload. -
The
path
is set to./orders.xml
, which is the path and name for theorders.xml
file that is loaded into theMultipartPart
. -
The
mime
parameter specifies the correct MIME type for the file. In this case, it isapplication/xml
. -
The
filename
can be changed to any value, it can be different from the actual input file’s filename.
Note that the output of this example is not compatible with the multipart/form-data
output type because it is just one part of a Multipart
structure. To create a valid multipart/form-data
output, use the Multipart::form()
function with one or more Multipart
files and/or fields.
Source
%dw 2.0
import dw::module::Multipart
output application/dw
var ordersFilePath = "./orders.xml"
---
Multipart::file{ name: "file", path: ordersFilePath, mime: "application/xml", fileName: "orders.xml" }
Input
A file called orders.xml
located in src/main/resources
with the following content:
<orders>
<order>
<item>
<id>1001</id>
<qty>1</qty>
<price>\$100</price>
</item>
<item>
<id>2001</id>
<qty>2</qty>
<price>\$50</price>
</item>
</order>
</orders>
Output
{
headers: {
"Content-Type": "application/xml",
"Content-Disposition": {
name: "file",
filename: "orders.xml"
}
},
content: "<?xml version='1.0' encoding='UTF-8'?>\n<orders>\n <order>\n <item>\n <id>1001</id>\n <qty>1</qty>\n <price>\$100</price>\n </item>\n <item>\n <id>2001</id>\n <qty>2</qty>\n <price>\$50</price>\n </item>\n </order>\n</orders>"
}
Example
This example inserts file content from a MultipartPart
into a Multipart
, resulting in a multipart/form-data
output. The example uses the form
function to create the Multipart
and uses file
to create a part.
The Multipart::form()
function accepts an array of Multipart
items, where each part can be created using the Multipart::field()
or Multipart::file()
functions.
Source
%dw 2.0
import dw::module::Multipart
output multipart/form-data
var ordersFilePath = "./orders.xml"
var myArgs = { name: "file", path: ordersFilePath, mime: "application/xml", fileName: "myorders.xml"}
---
Multipart::form([
Multipart::file(myArgs)
])
Output
------=_Part_5349_1228640551.1560391284935
Content-Type: application/xml
Content-Disposition: form-data; name="file"; filename="myorders.xml"
<?xml version='1.0' encoding='UTF-8'?>
<orders>
<order>
<item>
<id>1001</id>
<qty>1</qty>
<price>$100</price>
</item>
<item>
<id>2001</id>
<qty>2</qty>
<price>$50</price>
</item>
</order>
</orders>
------=_Part_5349_1228640551.1560391284935--
file(fieldName: String, path: String, mime: String = 'application/octet-stream', sentFileName: String = 'filename')
Creates a MultipartPart
data structure from a resource file.
This version of the file
function accepts String arguments in a comma-separated
list, for example:
Multipart::field("myFile", myClients, 'application/json', "partMyClients.json")
Parameters
Name | Description |
---|---|
|
A unique name (required) for the |
|
A path (required) relative to the |
|
The mime type (optional for strings), for example, |
|
An optional file name value for the |
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
%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")
])