I have an issue with the Base64 Encoding in ServiceNow?

srirampappagudi
Tera Expert

Hi Experts,

In Knowledge Management, I am trying to read the attachments. When I pull a dump in XML and try to decode the

attachments it works fine. But when I use the REST API and pull the attachments, I am not able to decode the file it

throws an error. When I try to compare the Base64 String in XML and REST API, both are different. Kindly let me know

how to get this issue resolved.

Thank you,

Sriram.

 

 

1 ACCEPTED SOLUTION

Hi Sriram,

Yes the actual data resides in sys_attachment_doc but not as whole but in chunks; so you won't get complete set of data from that table from single record.

https://snprotips.com/blog/2016/2/25/understanding-and-using-glideattachment

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

View solution in original post

18 REPLIES 18

sebastian_g_snc
ServiceNow Employee
ServiceNow Employee

Hi Sriram,

are you using the API to "pull" the attachment. Can you give some more details please?

Cheers

Hi Sebastian,

 

I am using the ServiceNow REST API to pull the Base64 encoded data. When I try decrypt it throws an error and I am not able to capture the

attachment which is usually a picture file but when I import it in XML and pull the data it works fine.

Not sure if there is any problem with pulling the data using REST API.

Please help.

Regards,

Sriram.

 

 

 

 

 

Hi Sriram,

So you are getting xml dump which contains base64encoded data of an attachment and using ServiceNow Attachment API you are trying to add that as an attachment to a record

Can you share sample script or data here?

Regards

Ankur

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

srirampappagudi
Tera Expert

Hi Ankur,

 

I just using the REST API and pulling the attachment data as shown below.

import java.io.*;
import okhttp3.*;
import org.json.*;
import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
import java.nio.charset.StandardCharsets;
public class GetKnowledgeAttachData{

//private static final String FILENAME = "C:\\ServiceNow\\ResultData.png";

static String text;
public String getKnowledgeAttachmentData(String TicketNumber)throws IOException{

System.setProperty("https.proxyHost", "proxydirect.tycoelectronics.com");
System.setProperty("https.proxyPort", "80");

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
//RequestBody body = RequestBody.create(mediaType, "{\"number\":\"INC0077949\"}");
Request request = new Request.Builder()
.url("https://teconnectivityqa.service-now.com/api/now/v1/table/sys_attachment_doc?sys_attachment.table_sys_id="+TicketNumber)
.get()
.addHeader("authorization", "Basic ")
.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "")
.addHeader("sysparm_display_value", "true")
.addHeader("sysparm_exclude_reference_link", "true")
.build();

Response response = client.newCall(request).execute();

ResponseBody b1=response.body();

byte[] arr=response.body().bytes();
System.out.println(arr.length);
String s1=new String(arr);
//System.out.println(s1);
return s1;
}

public static void main(String args[])throws Exception, IOException{

String out1=new GetKnowledgeAttachData().getKnowledgeAttachmentData("005a92a3db47ba40281038ff9d961940");
//System.out.println(out1);
JSONObject obj = new JSONObject(out1);
out1 = obj.get("result").toString();
//String pageName = obj.getJSONObject("pageInfo").getString("pageName");

//String result = new GetTicketInfo().getTicketData(Ticket_Number);
//JSONObject obj = new JSONObject(result);


//System.out.println(obj.getJSONObject("number"));

JSONArray arr = new JSONArray(out1);
for (int i = 0; i < arr.length(); i++)
{
text = arr.getJSONObject(i).getString("data");
text ="H4sIAAAAAAAAAHP3dLOwTBRhEGE4wsDw7dv32vqG6TNmtbZ3zl+46PSZMytWrj50+Mi7d+927d6zcdPmvgkTr1y5eufu3adPn5WUVfz//5+BFKD4kwVM64AIkJ0MrLkKKn0pmfMyOpcc/2DDwqTCZyKy/NO7g5sZJkx33Kptx9b1IPuAhGnPbkWXABcjoQvXNS6edhJXXh9usKirt1NwUXXdVTf2bTN8tLbE805eqGtqeibeQZWBp8GNpamgyI2RXYOBlSWHmYW1U3PGzFnKigzWAJvGC1TwAAAA";
System.out.println(text);
}
BufferedImage image = null;
//FileWriter fw = new FileWriter(FILENAME);
//BufferedWriter bw = new BufferedWriter(fw);
byte[] decoded = Base64.getDecoder().decode(text);
String s1=new String(decoded);
//ByteArrayInputStream bis = new ByteArrayInputStream(decoded);
//image=ImageIO.read(bis);
//bis.close();
//System.out.println(s1);
//FileWriter fw = new FileWriter(FILENAME);
//BufferedWriter bw = new BufferedWriter(fw);
//bw.write(s1);
//bw.close();

//byte[] data = Base64.getDecoder().decode(text.getBytes(StandardCharsets.UTF_8));
byte[] decodedStr = Base64.getDecoder().decode( text );
OutputStream stream = new FileOutputStream("C:\\ServiceNow\\ResultData.gif");
stream.write(decodedStr );
//File outputfile=new File("image.png");
//ImageIO.write(image, "png", outputfile);
//System.out.println(decoded.toString());


}

}

Let me know if I am missing anything.

 

Regards,

Sriram.