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.bodyvariable is not changed, the invoke policy keeps and passes the attachments to the server. - For a multipart request or response where the
message.bodyvariable is not changed, the result API action passes the attachments as the final response. - If the
message.bodyvariable is changed, attachments are discarded.
Retrieving an attachment
- GatewayScript examples
- Retrieve the attachment by index
location.
context.message.attachments[1]
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)
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, orXML NodeListobject: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 contentHello worldto 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].headersTo retrieve the value of the named header of the attachment:context.message.attachments[0].header.get();To delete the currentContent-Typeheader 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.
If the value type is array, thecontext.message.attachments[0].header.set('MyCookie', ['cookie1', 'cookie2']);MyCookieheader is noncoalesced and it is present with different entries in HTTP header.MyCookie: cookie1 MyCookie: cookie2