How to get binary data of attachment?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2018 07:27 PM
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?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2018 04:38 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2018 05:29 AM
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2018 12:05 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2018 03:31 AM
Attachments in servicenow are saved in base64 encoding. Converting it to binary would be messy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2018 05:26 AM
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.