REST Object Byte Stream

scottlmco
Giga Contributor

Hello,

I'm attempting to retrieve data from a REST interface that is returning a large binary object. However, when I receive it via the ServiceNow REST object and perform the response.getBody() function, I get a string representation of that object. Unfortunately, it's not a binary string (of 1s and 0s) but rather a string of non-UTF-8 characters like this one: � with a few recognized characters thrown in the mix.

Is the getBody() function only returning this incorrect String, or am I only seeing that when I try to output it? I've tried converting the payload returned by getBody() into a JavaScript TypedArray, but for whatever reason, any TypedArray is undefined on the ServiceNow back end... which leads me to another question. Is there any way to instantiate a TypedArray? Specifically, I would want to convert the blob I'm getting back into a Uint8Array to ultimately turn it into a base64 encoding.

The ultimate result I'm trying to get here is to retrieve a PNG image from a REST interface, translate it in memory to a base64 encoding, and set that as the src attribute of an image tag on the front end.

The issue is that if I pass the string representation of the getBody() payload to the front to try to decode it there, I'm passing the erroneous String representation of the data that can't be accurately converted back, as far as I know. But, I can't use any TypedArrays on the back end to try to perform this translation before it's turned into the erroneous String. Perhaps the ServiceNow REST object is only ever returning that String anyway, and I'm barking up the wrong tree. Does anybody have any insight/experience with this?

Scott

3 REPLIES 3

jason_mckee
ServiceNow Employee
ServiceNow Employee

Unfortunately, REST Message (and the underlying GlideHttpResponse object) do not support binary data at this time.   What you're seeing with .getBody() is the result of Java trying to parse your image data as a java.lang.String.



One possible solution would be to write a small http client (in the language of your choice) and install it on a MID Server.   You can call out to grab the image and then pull it into your instance as a file attachment from the MID.   Once it's in the instance as a file attachment, you can do your base64 conversions on it.


Hi Jason,

 

I also came across similar use case where I need to read image from s3 and write directly into web page using Rest API. I would like to avoid sys attachment way. 

I like your idea of http client on mid server but the question I have will that work without sys attachment also? Or is there way to convert into blob and render into widget

 

thank you in advance.

sandeep

pankaj_puniani
ServiceNow Employee
ServiceNow Employee

Hi Scott,



From Geneve onwards RESTMessagev2 supports handling binary response. It allows you to save it as an attachment on the platform. Please refer to this link:


RESTMessageV2 - saveResponseBodyAsAttachment(String tableName, String recordSysId, String fileName)



Thanks,


Pankaj