Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Outbound REST - Multipart/form-data

Rshear
Kilo Expert

Hi,

Currently working on an outbound REST to 3rd party tool (through a mid server). 3rd party requires mutipart to be sent when sending a file to attach/upload.

Test the POST call on postman and all ok. When in servericenow I'm getting no-where quick 🙂

That said I can get some kind of response just not the one I want (i.e. file uploaded). See REST content:

-----WebKitFormBoundaryAAA

Content-Disposition: form-data; name="file"; filename=${filename};

Content-Type:${contenttype}

${file}

-----WebKitFormBoundaryAAA

 

${contenttype} comes from the content type of the attachment on the sys_attachment table and ${file} is created using the below (which is in a script include):

-----------------------------------------------------------------------------------

var grAtt = new GlideRecord('sys_attachment');
grAtt.addQuery('table_sys_id', sys_id of attachment);
grAtt.query();

if(grAtt.next()){
var gsa = GlideSysAttachmentInputStream(grAtt.sys_id.toString());
var bytesContent = new Packages.java.io.ByteArrayOutputStream();

gsa.writeTo(bytesContent);
bytesContent.close();

//Get the file content
var mycontentStr = new Packages.java.lang.String(bytesContent.toByteArray());

i then set mycontentStr as a parameter to pass to the rest call.

 

Any help would be appreciated!...ive scoured the community and read a few things that multipart not supported and to use MId server script include however there seems to be conflicting views on this.

31 REPLIES 31

Hi Swetang,

     I have tried the above code ,it is working fine but I am facing one challenge, whenever I am creating the 

incident and attaching the files using Post API in Business Rule so it should post the right number of attachments but it is creating around 40 attachments on the third party tool. Can you please give your suggestions that what methods or changes should be implemented to Post the correct number of Attachments on the third party tool.

 

Hi @Amruta Dharmar1 , @Swetang 

 

i am also facing same issue, in the third party, i can see multiple file getting attached.

i have written an BR on sys_attachment table, and then triggered doUpload function, but it kept on hitting the loopback i belive.

may i please know how did u resolve ur issue ?

 

 

Hi @Swetang , 

may i know the endpoint to which attachment needs to be sent/attached is another servicenow instance or is it a third party tool ?

 

Hi, 

 

Thank you for this. The script is working for me, except that the PDF file I uploaded gets corrupted.  Error whenever I open the file:  Could not open because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

 

Please kindly advise. Thank you.

Swetang
Tera Contributor

Hi @Kamil  - Glad to know that this worked out for you 🙂 . Please mark the Reply helpful.