Send attachment in outbound REST integration

Saquib Mohammed
Mega Guru

I am trying to send an attachment via outbound REST integration. I have created a POST HTTP method with the following query parameters - 

{
"task_number":"${task_number}",
"task_cmdb_ci":"${task_cmdb_ci}",
"attachment_list": "${attachment_list}"

attachment_list is a list of object with attachment details.

In the business rule, i have the below script - 

var attachment_list = [];
if (attach.next()){
var gsa = GlideSysAttachmentInputStream(attach.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos, 0, 0);
baos.close();
var encData = GlideStringUtil.base64Encode(baos.toByteArray());
gs.log("This is encoded data of attachment " + encData);
var obj = {};
obj["encodedFile"] = encData;
obj["fileName"] = attach.file_name.toString();
obj["contentType"] = attach.content_type.toString();
attachment_list.push(obj);
}
gs.log("This is attachment_list outside if " + JSON.stringify(attachment_list));
gs.log("This is attachment_list outside if " + attachment_list);
r.setStringParameter('attachment_list', (attachment_list));
var requestBody = r.getRequestBody();
var response = r.execute();

 

The issue is that the attachment_list property is being sent as a string value while it needs to be sent as a list value. The payload that is being sent is in the below format:

{

    "attachment_list": "[{"encodedFile":"VGhpcyBpcyBhIHRlc3Q=","fileName":"Test1.txt","contentType":"text/plain"}]",

    "task_cmdb_ci": "UPS Hardware and Support",

    "task_number": "TASK000000042161"

}

While the attachment_list should be in the below formst - 

    "attachment_list": "[{"encodedFile":"VGhpcyBpcyBhIHRlc3Q=","fileName":"Test1.txt","contentType":"text/plain"}]",

 

1 ACCEPTED SOLUTION

Anish Reghu
Kilo Sage
Kilo Sage

Hi @Saquib Mohammed,

 

It appears that the issue is that the attachment_list property is being sent as a string value, when it should be sent as a list value. One way to fix this is to use JSON.stringify() to convert the attachment_list variable to a JSON string before passing it to r.setStringParameter('attachment_list',...).

Example:

 

 

r.setStringParameter('attachment_list', JSON.stringify(attachment_list));

 

 

Another way is to directly pass the attachment_list variable to r.setParameter() instead of r.setStringParameter()

 

Example:

 

 

r.setParameter('attachment_list', attachment_list);

 

This should be able to send the payload in the expected format as a list.

 

Kindly mark the response as Correct or Helpful.

Cheers,

Anish

View solution in original post

7 REPLIES 7

Anish Reghu
Kilo Sage
Kilo Sage

Hi @Saquib Mohammed,

 

It appears that the issue is that the attachment_list property is being sent as a string value, when it should be sent as a list value. One way to fix this is to use JSON.stringify() to convert the attachment_list variable to a JSON string before passing it to r.setStringParameter('attachment_list',...).

Example:

 

 

r.setStringParameter('attachment_list', JSON.stringify(attachment_list));

 

 

Another way is to directly pass the attachment_list variable to r.setParameter() instead of r.setStringParameter()

 

Example:

 

 

r.setParameter('attachment_list', attachment_list);

 

This should be able to send the payload in the expected format as a list.

 

Kindly mark the response as Correct or Helpful.

Cheers,

Anish

Thank you. I am going to try this. I had a follow-up question. Is there a way to print the request body? 

I tried this - gs.log("This is a log statement for requestBody " + rr.getRequestBody();

//r is an sn_ws.RESTMessageV2();

However, all it does it print the request payload str from POST HTTP method - 

{
"task_number":"${task_number}",
"task_cmdb_ci":"${task_cmdb_ci}",
"attachment_list": "${attachment_list}"

It does not show the actual values being sent across. I need something that prints the actual payload being sent to the external api 

@Saquib Mohammed 

 

Yes, there is a way to print the request body. One way to do this would be to use the getRequestBody() method of the RESTMessageV2 object to get the request body as a string, and then use the gs.log() method to print it out. For example:

 

 

var requestBody = rr.getRequestBody();
gs.log("Request body: " + requestBody);

 

 

Make sure that the log level of your script is set to DEBUG, so that it will print the request body.

Another way would be to use the getElements() method of the RESTMessageV2 object to get a list of the elements in the request body, then you can use a for loop to iterate over the elements and print them out.

 

 

var requestBody = rr.getElements();
for (var i = 0; i < requestBody.length; i++) {
  gs.log(requestBody[i].getName() + ": " + requestBody[i].getValue());
}

 

Make sure that the request body is in JSON format.

 

Reminder:

 

Kindly mark the response as Correct or Helpful.

Cheers,

Anish

Hi Anish
This doesn't seem to work

var requestBody = rr.getElements();
for (var i = 0; i < requestBody.length; i++) {
gs.log(requestBody[i].getName() + ": " + requestBody[i].getValue());
}

and all that the below statement does is print the request payload structure... Not the actual request with the values

var requestBody = rr.getRequestBody();
gs.log("Request body: " + requestBody);

Output

Request body: {
"task_number":"${task_number}",
"task_cmdb_ci":"${task_cmdb_ci}",
"attachment_list": "${attachment_list}"
}