REST Multipart/form-data binary image request body?

kuntal_mandal
Tera Contributor

I have been working on an integration and I would like to hear if anyone has successfully created a multipart/form-data outbound REST request with binary image data in Servicenow. The third party accepts multipart/form-post.

It works perfectly for plain/text file only. For any other file type I can send the attachment using the code below but I can't open that at the other end. So It seems there is an issue in sending file content using the binary format. If you have come across similar issue please let me know your view or recommendation please. 

The end point is https://${instance}/api/tickets/${ticket_id}/attachments

1. REST Message Http Header

Content-Type = multipart/form-data; boundary=myFileBoundary

2. In Content Block I have following

--myFileBoundary
Content-Disposition: form-data; name="attachments[]"; filename="${filename}"
Content-Type: ${ContentType}
Content-Transfer-Encoding: binary

${fileContent}
--myFileBoundary--

====================

I have a Business rule on sys_attachment table where it invokes this REST message and sets the parameters

${filename} = current.file_name

${ContentType} = current.content_type

For ${fileContent} I am using the code below in business rule

var gsa = new GlideSysAttachmentInputStream(current.sys_id);
var bytesContent = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(bytesContent);
bytesContent.close();

var mycontentStr = new Packages.java.lang.String(bytesContent.toByteArray());

So basically following lines of code in my business rule

r.setStringParameterNoEscape('ticket_id', <Third Part Incident Number>);
r.setStringParameterNoEscape('filename', current.file_name.toString());
r.setStringParameterNoEscape('ContentType', current.content_type.toString());
r.setStringParameterNoEscape('fileContent', mycontentStr);

It works for plain/text file. But when I send PDF/jpeg/PNG etc it gets attached to the third party ticket but I can't open it. The third party is suggesting that the binary content we are sending is not quite right. 

Would like  to hear if you have done something similar and any possible work around please.

Thank you.

2 REPLIES 2

Aleksandar4
Kilo Explorer

Did you manage to solve this somehow? I'm stuck in the same issue.

Thanks in advance!

mhieron
Tera Contributor

Still no one has a solution? Is it even possible without integration hub?