How to download Attachments from RITM through Scripted REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-26-2019 08:32 AM
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:
------------------------------------------------------------------------------------------------------------------------------------------------------
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;
response.setStatus(200);
response.setHeaders(hdrs);
var attachmentStream = new GlideSysAttachmentInputStream(attachment_sys_id);
writer.writeStream(attachmentStream);
}
}
})(request, response);
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-26-2019 09:06 PM
Hi Abhinav,
How are you seeing it in postman tool?
is it base64encoded data which you can see?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2019 05:53 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2019 08:26 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2019 09:49 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader