Unable to send attachment via REST

Pres
Mega Guru

I have been unable to send attachments via REST. The call sends data from an incident task table to an external system with no problems until I try to add the attachment. When I do, the Outbound HTTP Requests log shows a 200 message but the URL/endpoint is "http://snap/snap/scanrequest."  

I am trying to add "setRequestBodyFromAttachment." Ideally, I'd like to be able to use it here:

(function executeRule(current, previous /*null when async*/) {

try {
// Stops line breaks in journaling fields
var workinfo=current.work_notes.getJournalEntry(1).toString();
var notesarry = workinfo.split("\n");
var wnote=notesarry[1];
var comments=current.comments.getJournalEntry(1).toString();
var commentssarry = comments.split("\n");
var cnote=commentssarry[1];
// Stops line breaks in journaling fields

var r = new sn_ws.RESTMessageV2('------------', '------------');
r.setStringParameterNoEscape('number', current.number);
r.setStringParameterNoEscape('u_vendor_ref_no', current.u_vendor_ref_no);
r.setStringParameterNoEscape('state', current.state.getDisplayValue());
r.setStringParameterNoEscape('cmdb_ci', current.cmdb_ci.getDisplayValue());
r.setStringParameterNoEscape('assignment_group', current.assignment_group.getDisplayValue());
r.setStringParameterNoEscape('work_notes', wnote);
//r.setStringParameterNoEscape('comments', cnote);
r.setStringParameterNoEscape('sys_id', current.sys_id.toString());
r.setStringParameterNoEscape('u_am_location', current.u_am_location.getDisplayValue());
r.setStringParameterNoEscape('short_description', current.short_description);
r.setStringParameterNoEscape('u_am_email', current.u_caller.email.getDisplayValue());
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
}
})(current, previous);

I have tried just adding the attachment separate function in same Business Rule and when I do, it sends the incident data but does not send the attachment:

(function amRESTMessageV2() {
try {
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('post');
request.setEndpoint('----------');
request.setRequestBodyFromAttachment('----------');

var response = request.execute();
var httpResponseStatus = response.getStatusCode();

gs.print("http response status_code: " + httpResponseStatus);
}
catch (ex) {
var message = ex.getMessage();
gs.print(message);
}

})();

1 ACCEPTED SOLUTION

Pres
Mega Guru

That makes sense, but I'm not sure the problem is getting the attachment.

When I attach a document and check Outbound HTTP Requests, I only show it attempting to POST to http://snap/snap/scanrequest. The Response status is always 200 and the Request Length is different. If I remove/comment out the parts that try to send the attachment from the business rule, it sends the message to the correct endpoint.

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Did you check how the 3rd party is accepting the attachment data?

Did they share the sample request and response format? that should help you identify the correct way of sending the attachment content

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

In the Outbound Rest message we are sending record data from the business rule:

{
"u_inc_number":"${number}",
"state":"${state}",
"product":"${cmdb_ci}",
"assignment_group":"altametrics_jitb",
"work_notes":"${work_notes}",
"sys_id":"${sys_id}",
"short_description":"${short_description}",
"email":"${u_am_email}",
"location":"${u_am_location}"
}

The attachment data should be sent in the following format:

{
"u_attachments": [
{
"name": "",
"type": "",
"attachmentBody": //Base 64 Encoded
}
],

Hi,

In this case you will have to get the base64Encoded data of that attachment and include in that json key

Sample Script to get base64EncodedData

var   gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
gr.query();
if (gr.next()){
var StringUtil = new GlideStringUtil();
var sa = new   GlideSysAttachment();
var binData =   sa.getBytes(gr);
var encData =   StringUtil.base64Encode(binData);
gs.print(encData);
}

"attachmentBody": encData

Sharing link for help:

https://servicenow-docs.readthedocs.io/en/latest/Sending_Attachments_via_REST/

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Pres
Mega Guru

That makes sense, but I'm not sure the problem is getting the attachment.

When I attach a document and check Outbound HTTP Requests, I only show it attempting to POST to http://snap/snap/scanrequest. The Response status is always 200 and the Request Length is different. If I remove/comment out the parts that try to send the attachment from the business rule, it sends the message to the correct endpoint.