Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4153

Export imported word file using REST make the file corrupted

$
0
0

Update: Updated the problem to reflect recent learnings and make it more clear.

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.

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 to export back to OT, the file gets corrupted. Not sure what is happening when javascript put the content in a string and then put the content back into the multipart. Server side javascript in ServiceNow doesn't support Node.js, classes like File or FileReader so I cannot get hold of the stream, just the response body, with file content, as a string.

We managed to get this to work using powershell in an attempt to isolate what the problem can be. Hence, the problem is on the ServiceNow side, not the OT side.

What is the solution to get this import and export working in ServiceNow?

  • ServiceNow version: Washington patch 8
  • Javascript version: ES5

Example of request. This is ServiceNow specific and there doesn't seem to be any other way of make a REST request to third party from within a script:

var r2 = new sn_ws.RESTMessageV2(<REST Message>, <Method>);r2.setRequestHeader('Content-Type', 'multipart/form-data;boundary=--xxx');var boundary = '--xxx';var body = '--'+ boundary +'\r\nContent-Disposition: form-data; name="file"; filename="file.docx"\r\nContent-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n\r\n'+ fileContentStr +'\r\n'+'--'+ boundary +'--';r2.setRequestBody(body);var response2 = r2.execute();

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.


Viewing all articles
Browse latest Browse all 4153

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>