I have a scenario where I need to import a file from OpenText (OT) to ServiceNow and then export it back to OpenText (kind of moving the file which is not possible locally) using REST. We are talking about word/.docx files. I am not in control of the OT side so I need to solve this problem on my end in ServiceNow. However, there may be a general solution that is not specific for ServiceNow. But hey, if you have a ServiceNow specific solution just feel free to engage.
I want to take the response body and add it to the multipart when exporting it back to OT in the same format as when receiving it. This is not happening when just putting the response body to the multipart. Word is zip archive but it would be great if I could just "move" the data without doing advanced activities like unzipping, zipping, etc. I wonder if encoding can be a problem even though it is a zip archive. Word is most likely ISO 8859-1 (i assume before compressed?) and javascript strings are normally UTF-8. Not sure what is happening when javascript put the content in a string and then put the content back into the multipart.
What is the solution for exporting the file back?
I tried a few things while I was thinking it may be an encoding issue:
One attempt:
var result = [];for (var i = 0; i < responseBody.length; i++) { var charCode = responseBody.charCodeAt(i); result.push(charmap[charCode] || charCode);}fileContentStr = String.fromCharCode.apply(null, result);
Another attempt:
var fileBytes = responseBody.getBytes('ISO-8859-15'); //ISO 8859-15var fileContentStr = '';for (var i = 0; i < fileBytes.length; i++) { fileContentStr += String.fromCharCode(fileBytes[i]);}
Yet another attempt:
var fileBytes = [];for (var i = 0; i < responseBody.length; i++) { fileBytes.push(responseBody.charCodeAt(i) & 0xff);}fileContentStr = String.fromCharCode.apply(null, fileBytes);
Quite a few other attempts where classes isn't available on server side.