How to download Attachments from RITM through Scripted REST API

abhinavnishant
Tera Contributor

I need to create a Scripted REST API which shall accept the RITM numbers and return the attachments associated with the given RITM.

This is my Sample Code:

------------------------------------------------------------------------------------------------------------------------------------------------------

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
 
 var queryParams = request.queryParams;
 var queryRITM = queryParams.RITM;
 //queryRITM = queryRITM.split(',');
 
 var gr = new GlideRecord('sc_req_item');
 gr.addQuery('number',queryRITM);
 gr.query();
 while (gr.next()){
  
  var ga = new GlideRecord('sys_attachment');
  ga.addQuery('table_sys_id',gr.sys_id.toString());
  ga.query();
  while(ga.next()){
  var hdrs = {},
        attachment_sys_id = ga.sys_id;
    hdrs['Content-Type'] = "'"+ ga.content_type+ "'";
    response.setStatus(200);
    response.setHeaders(hdrs);
    var writer = response.getStreamWriter();
    var attachmentStream = new GlideSysAttachmentInputStream(attachment_sys_id);
    writer.writeStream(attachmentStream);
  }
  }
 })(request, response);
---------------------------------------------------------------------------------------------------------------------------------------------------------------
 
I am getting the download option in Postman, but the returned file has the name "response" without and extension.
Can I have the filename same as the filename in "sys_attachment" table?
 
What are the alternate ways to get the attachment from an RITM?
I did explored the Attachments API and I see I need to pass the sysID of the file. 
Is there a way I can use/reuse the Attachment API in Scripted REST API and get to download the related attachments?
Any help will be appreciated.
Thanks,
-Abhinav
10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Abhinav,

How are you seeing it in postman tool?

is it base64encoded data which you can see?

Regards

Ankur

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

Yes I do see a base64 encoded data. Is a long string of random characters. 

find_real_file.pngSomething Like this

Hi Abhinav,

That doesn't seem to be base64encoded string. because base64encoded data is combination of characters and numbers and not the special characters.

I think you need to send proper base64encoded data

var StringUtil = new GlideStringUtil();

var gsa = GlideSysAttachmentInputStream(ga.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos);

var baos = new Packages.java.io.ByteArrayOutputStream();

var base64EncodedData =  StringUtil.base64Encode(baos.toByteArray());

 writer.writeStream(base64EncodedData);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Abhinav,

Any update on this?
Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.

Regards
Ankur

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