Convert REST response to Base64
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2017 11:08 PM
Hi,
I am trying to fetch JIRA attachment through JIRA rest API and JIRA rest API provide me url (/secure/attachment/{attachment-id}/{attachment-name})
of the content. Further, I am trying to get attachment itself as below.
// code
var targetInstanceURL = "https://jira.****.com/secure/attachment/261255/";
var targetUserID = "username";
var targetUserPassword = "pass";
var sa = new GlideSysAttachment();
var StringUtil = GlideStringUtil();
sendAttachments();
function sendAttachments() {
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("get");
attachmentMessage.setBasicAuth(targetUserID, targetUserPassword);
attachmentMessage.setEndpoint(targetInstanceURL);
var response = attachmentMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(responseBody);
var base64string = GlideStringUtil.base64Encode(responseBody);
gs.log("2 " + base64string );
.
.
.
}
Response Header:
Content-Disposition: inline; filename*=UTF-8''tets.jpg;
Content-Length: 2781
Content-Type: image/jpeg
OUTPUT:
gs.log(responseBody) is something like ���� JFIF xx��C
gs.log("2 " + base64string ) is something like 77+977+977+977+9ABBKRklGAAEBAQB4AHgAAO+/ve+/vQBDAAIBAQIBAQICAgICAgICAwUDAwMDAw
Cases:
1. For text file my code is working fine and I am able to add attachment in service now table.
2. For image and pdf my code is working but saved file is showing as corrupt file.
Problem:
As per my understanding for image response conversion to base64 is not correct and GlideStringUtil.base64Encode() methode is not encoding image/pdf response to base64.
Question:
1. How to convert image/pdf response to base64 or ByteArray?
2. Any other approach to store attachment in service now from JIRA?
Thanks in advanced.
Regards,
Iqbal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2017 04:39 AM
any luck finding the solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2021 02:06 AM
I am able to save attachment through saveResponseBodyAsAttachment.
//code
var targetInstanceURL = endpoint;
var targetUserID = username;
var targetUserPassword =password;
var sa = new GlideSysAttachment();
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("get");
//Mid server if you want to run it thorugh mid server
var midServer = gs.getProperty("midServerName", "");
if(midServer){
attachmentMessage.setMIDServer(midServer);
}
//
attachmentMessage.setBasicAuth(targetUserID, targetUserPassword);
attachmentMessage.setEndpoint(targetInstanceURL);
attachmentMessage.saveResponseBodyAsAttachment("incident", incSysId,fileName,fileType);
var response = attachmentMessage.execute();
var httpStatus = response.getStatusCode();
var newAttachmentSysId = response.getResponseAttachmentSysid();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2017 05:10 AM
var att ='';
var grSysAtt = new GlideRecord('sys_attachment');
grSysAtt.addQuery('table_sys_id',current.sys_id);
grSysAtt.addQuery('table_name', 'incident');
grSysAtt.addQuery('u_file_transferred',false);
grSysAtt.query();
while(grSysAtt.next()){
var sa = new GlideSysAttachment();
var StringUtil = GlideStringUtil;
var binData = sa.getBytes(grSysAtt);
var encData = StringUtil.base64Encode(binData);
Could you please try this ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2017 05:27 AM
Hi Iqbal,
The attachment you are getting must be already in base64 encoded format. you need to decode it using base64 and then attach as attachment. This is because attachment content is sent as base64encoded data from source to destination.
// code
var targetInstanceURL = "https://jira.****.com/secure/attachment/261255/";
var targetUserID = "username";
var targetUserPassword = "pass";
var sa = new GlideSysAttachment();
var StringUtil = GlideStringUtil();
sendAttachments();
function sendAttachments() {
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("get");
attachmentMessage.setBasicAuth(targetUserID, targetUserPassword);
attachmentMessage.setEndpoint(targetInstanceURL);
var response = attachmentMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(responseBody);
var base64string = GlideStringUtil.base64Encode(responseBody);
gs.log("2 " + base64string );
var value = GlideStringUtil.base64Decode(responseBody);
// the value variable has to be used to create image or other file
// i have assumed that responseBody contains the base64encoded data if not then you need to parse the json response body and fetch the base64encoded stream
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader