Need assistance with decoding a file with StringUtil.base64DecodeAsBytes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2020 12:22 AM
Hi -
I need to submit an attachment from a ServiceNow instance to a ServiceNow instance via rest.
In the following code snippet, the following widget server side code works perfectly:
var attachment = new Attachment();
var wr = attachment.write(dataTable, data.incSysID, file.filename, '', StringUtil.base64DecodeAsBytes(file.file));
...Except I can't use it, as it doesn't work when run as guest. For my purposes, I need to send the attachment via rest with a hardcoded basic auth so that guests can do it too.
However, I can't get the following rest code to work. It kind of works: it uploads a "file" to the right table with the right name and data type, but the file is only a couple bytes long.
StringUtil.base64DecodeAsBytes(file.file), for some reason, is NOT giving me the entire data string. I can't find any info on StringUtil.base64DecodeAsBytes, and I'm not sure what I'm doing wrong.
function addFiles() {
var dataTable = "incident";
//Attach files to ticket
input.nsf_files.forEach(function(file) {
var StringUtil = new GlideStringUtil();
if (file.filename) {
//this works, but runs as whoever is logged in on the portal, so if guest it won't work.
// var attachment = new Attachment();
// var wr = attachment.write(dataTable, data.incSysID, file.filename, '', StringUtil.base64DecodeAsBytes(file.file));
gs.log(StringUtil.base64DecodeAsBytes(file.file));//This is returning [B@1f34781
//this should be a 4kb file
//since the data is way too small, of course this isn't working
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setBasicAuth(user, passwd);
restMessage.setHttpMethod("POST");
restMessage.setEndpoint(instanceURL + 'api/now/attachment/file?table_name=' +
dataTable + '&table_sys_id=' + data.incSysID + '&file_name=' + file.filename);
restMessage.setRequestHeader("Accept", "application/json");
restMessage.setRequestHeader('Content-Type', file.filetype);
restMessage.setRequestBody(StringUtil.base64DecodeAsBytes(file.file));
restMessage.setRequestBody(requestBody);
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.addInfoMessage("file uploaded response statuscode: "+httpStatus);
}
});
}
Please help! How can I get access to the entire "file.file" attachment data here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2020 10:49 AM
I slightly recoded using your advice and got a lot further. It's still not quite right, tho; I reposted the question phrased differently, and with a bit more experimental testing and results here:
Looks like if I encode/decode a 15KB text file as in the above link, it works fine.
However, I encoded/decoded 2 binary files (4 and 5 KB each), and 1 is missing 1 byte of data in the middle... and the other is missing 2 bytes of data right next to each other at about 1/3 the way through.
This is driving me nuts!