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

srirampappagudi
Tera Expert

Hi Ankur,

 

Yes you are right.... Am I missing anything...

 

Regards,

Sriram.

Hi Sriram,

This is sample java code I used earlier to fetch attachment from ServiceNow and create file using java code.

highlighted in bold is the sys_id of the sys_attachment record which contains the file; since I was knowing it was pdf file so I have give file name as pdf

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.net.URLEncoder;

public class RESTAPI {

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

URL url = new URL("https://instanceName.service-now.com/api/now/attachment/e84c64d84fc32200fc11fa218110c7b4/file");
String passwdstring = "rest.user:rest.user"; // username:password
Encoder encoder = Base64.getEncoder();
String encoding = encoder.encodeToString(passwdstring.getBytes(StandardCharsets.UTF_8) );


HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Basic " + encoding);
conn.setRequestProperty("Accept", "application/json");
InputStream is = conn.getInputStream();

InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";

FileOutputStream outstream = null;
File outfile =new File("E:\\Projects\\Test\\Testing\\MyOutputFile12.pdf");
outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];

int length;

while((length = is.read(buffer)) > 0){
String trimdLine = line.trim();
outstream.write(buffer, 0, length);
}
outstream.close();
}

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 Ankur,

 

The actual data is in the table 'sys_attachment_doc". why am I not able to pull the data and decrypt it from there?

Is that not possible?

Regards,

Sriram.

 

 

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

Hi Ankur,

 

Could you pls let me know where have you run this code?

 

 

Regards,

Nikita