Get Attachments through REST API

Aruna Sree Yela
Tera Guru

Hi,

 

3rd party tool is calling servicenow to get attachments of RITM in binary stream.

 

I want to achieve this using script include and Scripted REST API. How to achieve this

 

Thanks

1 ACCEPTED SOLUTION

@Aruna Sree Yela 

I am able to get the base64 data using background script in global scope

You can check the script and enhance

I believe if the base64 string is huge then it might not work while setting the response of scripted rest api

var attGr = new GlideRecord('sys_attachment');
  attGr.addQuery('sys_id', 'b45f80356f102100758ecb512e3ee485');
  attGr.query();
  if(attGr.next())
        {
   var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); //few versions support the first one, other supports second
   var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment());
   var attachmentData = gsa.getBytes(attGr);
   var attachment = String(Packages.java.lang.String(attachmentData));
   gs.info(attachment); //the data in the file will be printed as String
   var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
   gs.info(encData);
}

Output:

AnkurBawiskar_0-1739272573205.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

20 REPLIES 20

Tony Chatfield1
Kilo Patron

Hi, if your third party app is making a REST call to retrieve the attachment then I would expect it to be a call to the sys_attachment table, and I do not see any reason to need a script-include or scripted rest API as part of the process.

Have you looked at GlideSysAttachment getContentBase64() ?

GlideSysAttachment | ServiceNow Developers

 

If I have misunderstood? perhaps you could clarify your requirement.

Hi @Tony Chatfield1 , thank you for the response!

 

The reason behind using script include is we have multiple APIs for different use where we are calling all of them from a common script include.

 

Below is the script include function:

 

getAttachment: function(request) {
var attachmentSysId = request.queryParams.sys_id;
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('sys_id', attachmentSysId);
attachmentGR.query();
if (attachmentGR.next()) {
                var attachment = new GlideSysAttachment();
                var content = attachment.getBytes(attachmentGR);
                var contentType = attachmentGR.content_type;
                var base64 = GlideStringUtil.base64Encode(content);
                var binaryData = GlideStringUtil.base64Decode(base64);
                gs.log('base64: ' + base64);
                gs.log('binaryData: ' + binaryData);
            }
            return binaryData;
}

 

 

Calling that in Scripted REST API

 

var siUtil = new SI_Utils();
var reqResponse = siUtil.getAttachment(request);
response.setContentType(reqResponse.contentType);
response.getStreamWriter().writeBytes(reqResponse.content);

 

 

I'm able to see the base64 and binaryData logs, but rest explorer is showing "". Not getting the binary value. Seems like something is wrong while calling it in Scripted REST API. Could you please correct me where I missed it.

@Aruna Sree Yela 

what's your business requirement to send binary data?

In ServiceNow attachments are stored with base64 encoding.

You can only get a string that represents binary, but not actual binary object.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar , Got it. Now below is my function from Script Include to get base64 , then how can I call that and set the response body in Scripted REST API - Get method. Because I'm getting the value in logs but in Explorer getting error like

 

message: "Script Evaluation Exception",

details: "Cannot convert xebywynuemxyzdbednuic4....................."

 

getAttachment: function(request) {
            var attachmentSysId = request.queryParams.sys_id;
            gs.log("attachmentSysId " + attachmentSysId);

            var attachmentGR = new GlideRecord('sys_attachment');
            attachmentGR.addQuery('sys_id', attachmentSysId);
            attachmentGR.query();
            if (attachmentGR.next()) {
                var attachment = new GlideSysAttachment();

                var bytes = attachment.getBytes(attachmentGR);
                var base64 = GlideStringUtil.base64Encode(bytes);

                gs.log('base64: ' + base64);
            }
            return base64;
}