DataPower API Gateway
only

Manipulating attachments in a message object

The message object provides APIs and properties to manipulate message attachments.

If the assembly includes a parse policy, attachments in the request or response of a multipart message can be manipulated and sent back to the client. By default, the response is sent to the client in multipart format.
Restriction: You cannot add an attachment to a request or response which does not contain existing attachments.
Alternatively, you can clear context.message.attachments to discard the attachments and send only message.body to the client.
Note: If the assembly does not include a parse policy, the following processing rules apply.
  • For a multipart request where the message.body variable is not changed, the invoke policy keeps and passes the attachments to the server.
  • For a multipart request or response where the message.body variable is not changed, the result API action passes the attachments as the final response.
  • If the message.body variable is changed, attachments are discarded.

Retrieving an attachment

GatewayScript examples
Retrieve the attachment by index location.
context.message.attachments[1]
Retrieve the attachment by looking up the value of pic1 in the Content-Id header.
context.message.attachments['cid:pic1']
Retrieve the attachment by looking up the value of uri1 in the Content-Location header.
context.message.attachments['uri1']

Appending an attachment to the message

GatewayScript example
var headers = JSON.parse ( '{ "Content-Type": "text/xml; charset=utf-8", "Content-Id": "<data>" }' );
context.message.attachments.append(headers, 'Hello world.');

Retrieving the number of attachments in an array

Example
for (var i = 0; i < context.message.attachments.count; i++) {
  context.message.attachments[i].header.set('X-Foo', 'bar');
}

Removing an attachment from the message

GatewayScript examples
Remove the attachment by index location.
context.message.attachments.remove(1)
Remove the attachment by looking up the value of pic1 in the Content-Id header.
context.message.attachments.remove('cid:pic1')
Remove the attachment by looking up the value of uri1 in the Content-Location header.
context.message.attachments.remove('uri1')

Reading, accessing, and manipulating the body of an attachment

GatewayScript examples
To read the payload of an attachment in binary and return the contents as either a Buffer, Buffers, JSON, or XML NodeList object:
context.message.attachments[0].body.readAsBuffer(function(errorObject,bufferObject){})
context.message.attachments[0].body.readAsBuffers(function(errorObject,buffersObject){})
context.message.attachments[0].body.readAsJSON(function(errorObject,jsonObject){})
context.message.attachments[0].body.readAsXML(function(errorObject,nodeList){})
To write content Hello world to the current payload of the attachment:
message.attachments[0].body.write("Hello world")

Manipulating the headers of an attachment

GatewayScript examples
To retrieve the values of the whole attachment headers:
context.message.attachments[0].headers
To retrieve the value of the named header of the attachment:
context.message.attachments[0].header.get();
To delete the current Content-Type header of the attachment.
context.message.attachments[0].header.remove('Content-Type');
You can set the value of the named header of an attachment. The header can be coalesced and noncoalesced. Take the following code as an example.
context.message.attachments[0].header.set('MyCookie', ['cookie1', 'cookie2']);
If the value type is array, the MyCookie header is noncoalesced and it is present with different entries in HTTP header.
MyCookie: cookie1
MyCookie: cookie2