Send API Multipart attachment to thirdparty

BenG
Tera Guru

Hello,

 

I'am trying to send a API multipart attachment by business rule. I would like to do that without MID server but I have no idea how to do that.

Any idea ?

Thanks for your help

5 REPLIES 5

Maik Skoddow
Tera Patron

Hi @BenG 

there is a article explaining it and providing the code: Script to send multipart (including text and binary) RestAPI

And also there is a ready-to-use-solution in the Share: MultiPart Upload Helper 

Maik

Rajdeep Ganguly
Mega Guru


Sure, you can send a multipart attachment via a business rule in ServiceNow without using a MID server. Here's a step-by-step guide:

1. Create a new Business Rule.
2. Set the 'When to run' section according to your requirements.
3. In the 'Advanced' section, write a script to send the API multipart attachment.

Here's a sample script:

javascript
(function executeRule(current, previous /*null when async*/) {
try {
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('post');
request.setEndpoint('your_endpoint_url');
request.setRequestHeader('Content-Type', 'multipart/form-data');
request.setRequestHeader('Accept', 'application/json');
request.setAuthenticationProfile('oauth2', 'your_oauth_profile_id'); //set your OAuth2 profile

var attachment = new GlideSysAttachment();
var attachmentGR = attachment.getAttachmentRecord('sys_attachment', 'your_attachment_sys_id'); //set your attachment sys_id
var inputStream = attachmentGR.getContentStream();
request.setRequestBodyFromStream(inputStream);

var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}
})(current, previous);


Please replace 'your_endpoint_url', 'your_oauth_profile_id', and 'your_attachment_sys_id' with your actual values.

Remember:

- This script uses the RESTMessageV2 API, which is available starting with the Eureka release.
- The 'setRequestBodyFromStream' method is used to set the request body from an InputStream object.
- The 'getContentStream' method is used to get an InputStream object for the attachment content.
- The 'execute' method is used to send the REST message to the endpoint.
- The 'getBody' and 'getStatusCode' methods are used to get the response body and status code, respectively.

Please test this in a sub-production instance first before implementing it in a production environment.


nowKB.com

Ramesh Lohar
Kilo Guru

Sure, you can send a multipart attachment via a business rule in ServiceNow without using a MID server. Here's a step-by-step guide:

1. Create a new Business Rule in ServiceNow.
2. Set the 'When to run' section according to your requirements.
3. In the 'Advanced' section, write a script to send the API multipart attachment.

Here's a sample script:

javascript
(function executeRule(current, previous /*null when async*/) {
try {
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('post');
request.setEndpoint('your_endpoint_url');
request.setRequestHeader('Content-Type', 'multipart/form-data');
var attachment = current.getAttachment('sys_id_of_attachment');
request.setRequestBodyFromAttachment(attachment);
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}
})(current, previous);


Please replace 'your_endpoint_url' and 'sys_id_of_attachment' with your actual endpoint URL and the sys_id of the attachment you want to send.

Please note:

- This script uses the RESTMessageV2 API, which is available starting with the Eureka release.
- The 'setRequestBodyFromAttachment' method is used to add the attachment to the request body.
- The 'execute' method is used to send the request.
- The 'getBody' and 'getStatusCode' methods are used to get the response body and status code, respectively.
- The 'catch' block is used to handle any exceptions that may occur during the execution of the script.

Remember to test your business rule thoroughly before deploying it to a production environment.


nowKB.com

BenG
Tera Guru

Hello,

Thank you for your answer.

I found my way creating Action and flow in flow designer. It works well.

Cheers,