How to read and save rest image/png response?

Felix20
Mega Guru

Hi everyone,

we have a service that response with a rest call with a qr code image png.

The image will be directly in the body without any json style ect.

How to use that byte array / image from rest to save it as an attachment on a record?

 

When logging it, it puts this:

�PNG

IHDRt ���IDATx^��A��0�ޠ��enPb{&����]��ɼ������c���(J���
��9��ߚ���}���,qٓ/�s֠~�yN~��Ρ��C�:X�.��@I]+Z��z�U�
\�|��j�,E�p��*~`���n�@� ���x9ؑ*¬���vp��z����p��=��H��I�T�@?�l�(�!J��(���]ȉ O(IEND�B`�

 

Best regards

4 REPLIES 4

Chuck Tomasi
Tera Patron

Can you share the raw REST payload (or a snippet of it)? These are almost always in text. That implies that the image is encoded (base64 perhaps) and needs to be decoded.

Let us know what you have tried already. Sharing code and results is most appreciated.

Hi Chuck,

thank you for your update. I know that that the rest reponse type is image/png type and the request body is a byte array. I found the saveResponseBodyAsAttachment function on RESTMessageV2 that works perfectly for me.

By the way, me and my team will be at the orlando conference and we would be pleased to have a coffee with you and a short chat. 

Best Regards, and looking forward to Orlando conference

 

Nidhi Garg
ServiceNow Employee
ServiceNow Employee

Hey I am facing the same issue. Can you help me with that please?

Tom Cullen
ServiceNow Employee
ServiceNow Employee

Hello all,

Last week I had the need to generate a QR code and save it as an attachment.  When my returned data came back like this I knew I was close. After a couple of attempts at conversion I came across this Community post and it pointed me in the right direction (Thank you Chuck!) ... but not exactly.

find_real_file.png

Turns out there is an API for this specific use case.  

saveResponseBodyAsAttachment(String tableName, String recordSysId, String fileName)

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server/sn_ws-namespace/c_RESTMessageV2API#r_RMV2-saveRespBodyAsAttach_S_S_S?navFilter=saveres

 

Here is an example of the code I used:

 

find_real_file.png

Hope this helps !!!