file

DataWeave 2.2 is compatible and bundled with Mule 4.2. Standard Support for Mule 4.2 ended on May 2, 2021, and this version of Mule will reach its End of Life on May 2, 2023, when Extended Support ends.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

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:

Multipart::file({ name: "myFile", path: "myClients.json", mime: "application/json", fileName: "partMyClients.json"})

Parameters

Name Description

opts

Array of objects that specifies:

  • A unique name (required) for the Content-Disposition header of the part.

  • A path (required) relative to the src/main/resources project path for the Mule app.

  • mime (optional for strings) for the mime type (for example, application/json) to apply to content within the part. This setting cannot be used to transform the input mime type.

  • An optional fileName value for the filename parameter in the part’s Content-Disposition header. Defaults to the string "filename" if not supplied. This value does not need to match the input file name.

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

%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.

clients: {
  client: {
    id: 1,
     name: "Mariano"
   },
   client: {
     id: 2,
     name: "Shoki"
  }
}

Output

------=_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:

Multipart::field("myFile", myClients, 'application/json', "partMyClients.json")

Parameters

Name Description

opts

Array of objects that specifies:

  • A unique name (required) for the Content-Disposition header of the part.

  • A path (required) relative to the src/main/resources project path for the Mule app.

  • mime (optional for strings) for the mime type (for example, application/json) to apply to content within the part. This setting cannot be used to transform the input mime type.

  • An optional fileName value for the filename parameter in the part’s Content-Disposition header. Defaults to the string "filename" if not supplied. This value does not need to match the input file name.

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")
])

Input

A file called myClients.json and located in src/main/resources with the following content.

clients: {
    client: {
      id: 1,
      name: "Mariano"
    },
    client: {
      id: 2,
      name: "Shoki"
    }
  }

Output

------=_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--

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub