Outbound REST - Multipart/form-data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2018 12:54 AM
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.
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2020 02:31 AM
Thx again for your help
BR,
Kamil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2020 04:29 AM
Hey Swetang,
I am using the same approach for attachment ebonding with Remedy tool but when I am sending any excel or word document from servicenow, the file getting attached in Remedy seems to be in unsupported format. I am converting the attachment to base64encoded, below is the code to convert the attachment to multipart form-data.
var writer = response.getStreamWriter();
writer.writeString("----WebKitFormBoundary" + boundaryValue + "\r\n");
var fileName = attachmentGR.file_name.toString();
writer.writeString('Content-Disposition: form-data; name="' + formName + '"; filename="' + fileName + '"\r\n');
// set the content type for this part of the datastream
var contentType = attachmentGR.content_type.toString();
writer.writeString('Content-Type: application/octet-stream\r\n');
writer.writeString('\r\n');
var sa = new GlideSysAttachment();
var binData;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', attachmentID);
gr.query();
if (gr.next()) {
binData = sa.getBytes(gr);
}
var inputStream = GlideStringUtil.base64Encode(binData);
writer.writeString(inputStream);
// finalize the payload by ending the boundary
writer.writeString("\r\n----WebKitFormBoundary" + boundaryValue + "--\r\n");
I am not sure if I am missing out any thing, need some guidance to achieve this requirement.
Thank you in advance!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2020 05:05 AM
Please just mount the binary data only. Donot convert it into base64. It shall work. Please check and let me know if it is working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2020 09:45 PM
Hey Swetang,
Thanks for your quick response. I have modified the code as below.
var sa = new GlideSysAttachment();
var binData;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', attachmentID);
gr.query();
if (gr.next()) {
binData = sa.getBytes(gr);
}
writer.writeString(binData);
However this is also not working. The attachments are getting uploaded as unsupported format. Can you please let me know if I am doing wrong somewhere?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2020 01:40 AM
var contentType = attachmentGR.content_type.toString();
writer.writeString('Content-Type: application/octet-stream\r\n');
