Contact Us 1-800-596-4880

field

field(opts: {| 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 creates a Multipart data structure that contains parts.

Source

%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

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

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(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 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--