How to get binary data of attachment?

PhoenixMing0912
Giga Expert

Hi all,

There is a sample coding which is from service now document.

var attachment_sys_id = 'bacd7034dbeaa3403aff6a4948964f28';
attachmentStream = new GlideSysAttachmentInputStream(attachment_sys_id);

However, attachmentStream is just something like "[B@89e840" but I want the real binary data.

I have read through documents but I didn' t find the answer.

Can anyone help me out?

15 REPLIES 15

If you want to transfer Binary to third part, you need to use "setRequestBodyFromAttachment" method of RESTMessageV2, 

 

setRequestBodyFromAttachment("sysIdOfAttachment")

 

This send the binary in request payload of web service

 

Note: Please mark reply as correct if it answers your question

I would try this, but actually I need to send some fields and attachment both, by using content type multi-part form data.

Now I concatenate all the fields and base64/binary of attachment into a string and set it into body by method "setRequestBody()". 

 

----Updated Content-----

I have tried this and got a 500 error.

request.setRequestBodyFromAttachment('bacd7034dbeaa3403aff6a4948964f28'); //sys id
request.setQueryParameter("xxx","xxx");

My third-party service wants the content in a mulitpart format.

"Content-Type","multipart/form-data; boundary=----4ZLQTMdAcvY01zbc46e699db"

Updated.

I have also tried "setRequestBodyFromAttachment("sysIdOfAttachment")" more times these days.

Yes, setRequestBodyFromAttachment("sysIdOfAttachment") send out in binary, but after I received the binary and convert to a bytes Array on my Java server, I found the bytes format is different from that of raw file. I think that's the reason why it cannot be uploaded to my third party server correctly.

Anurag Tripathi
Mega Patron
Mega Patron

Attachments in servicenow are saved in base64 encoding. Converting it to binary would be messy. 

-Anurag

I tried to send this base64 encoding to a Java server where I convert the encoding to binary, and then the binary was accepted by the third party server I mentioned.

So that's the reason I think the binary I got in service now side was wrong.