Contact Us 1-800-596-4880

field

DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended.

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.

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

opts

Array of objects that specifies:

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

  • A value (required) for the content of the part.

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

  • An optional fileName value that you can supply to the filename parameter in the part’s Content-Disposition header.

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

%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

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

Multipart::field("order", myOrder,"application/json", "order.json")`

Parameters

Name Description

opts

A set of parameters that specify:

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

  • A value (required) for the content of the part.

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

  • An optional fileName value that you can supply to the filename parameter in the part’s Content-Disposition header.

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

%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

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