Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Convert REST response to Base64

iqbalkhokhar
Giga Contributor

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

6 REPLIES 6

Thanks you all for response.


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();


Hi Ankur,

We are receiving the responseBody in Binary format. We want the responseBody to be attached as a pdf file to a current record. So using below code.

var base64string = GlideStringUtil.base64Encode(responseBody);

var attCreator = new GlideRecord('ecc_queue');
     attCreator.agent = "AttachmentCreator";
     attCreator.topic = "AttachmentCreator";
     attCreator.name =   "test1.pdf" + ":" + 'pdf';
     attCreator.source = tablename+":"+recordsys_id;

attCreator.payload = base64string;
     attCreator.insert();

 

But it is attaching an empty file. when I try below code:

var value = GlideStringUtil.base64Decode(responseBody);

var attCreator = new GlideRecord('ecc_queue');
     attCreator.agent = "AttachmentCreator";
     attCreator.topic = "AttachmentCreator";
     attCreator.name =   "test1.pdf" + ":" + 'pdf';
     attCreator.source = tablename+":"+recordsys_id;

attCreator.payload = base64string;
     attCreator.insert();

 

This is returning corrupted file and unable to open it. Could you please suggest an alternative way to convert binary format response into an attachment pdf.

 

Thanks.