Download file using REST API - Binary response problem

Sal12
Tera Expert

I am using the REST Step in the Flow Designer Action to do a REST call to a remote Apache Server.

The Response Body is a raw string of binary , which is basically a file in raw format (binary).

So,  I store that raw string of binary in the output variable of the Action. I also strore the file name and status as well for later use.

Now I am using a client side script to call a script include using GlideAjax. I call that Flow Designer Action on that Script Inlcude and use the raw string of binary data returned from the the Flow designer Action and encode it using gs.Base64Encode. I put that encoded value, the File name into an array then JSON stringify it and return it to the client side.

 

On the client I parse the answer and to get my base64 string and file name out. I convert that base64 string to binary again using atob method then eventually save the file on my computer using the Blob constructor and createObjectURL methods.

 

The file downloads and saves, but the content is not readable.

After a lot of fiddling I was able to see what the problem might be.

I compared the base64 encoded string during the upload and the base64 string durign downlod process.

I see that the base64 during download is corruted or you can also say it got mutated.

 

So, for example:

This is just a made up string, but the intent is to convey the issue here.

  • Upload base64 string:  Uessssfffgge+CvvbfRRttt
  • Download base64 string:  Uessssfffggh+/43rtyyvbfRRttt

Would someone know how to solve this? Is there a problem with ServiceNow reading the binary raw string it self, and that's why this issue?

I can download the same file from Postman, no issues there.

 

 

 

 

1 REPLY 1

Vikram Gupta
Tera Contributor

Hi @Sal12  how you were able to use  Blob constructor and createObjectURL methods in servicenow I want to alse save the attachment in servicenow