SEND ATTACHMENT THROUGH REST API

nandika 1
Tera Contributor

Hi 

I want to send attachment through Rest Api.can anyone please help me on it .Kindly guide on how to write the rest api and business rule for it

1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron
Giga Patron

Hi @ nandika 1 ,

Use below script in business rule  to sent attachment to third party api

 

 

var attachmentid = '';
		var gr = new GlideRecord("sys_attachment");
		gr.addQuery("table_sys_id", current.getValue('sys_id'));
		gr.query();
		if (gr.next()) {
			attachmentid = gr.getValue('sys_id');//getting attachent sys_id
		}
		var tosend = JSON.stringify(body);
		var request = new sn_ws.RESTMessageV2('Ottos test instance', 'Otto_Post');
		request.setRequestHeader("Accept", "application/json");
		request.setRequestHeader('Content-Type', 'application/json');
		request.setRequestBody(tosend);
		request.setRequestBodyFromAttachment(attachmentid);//setting attachment ot thirdparty
		request.setWorkflow(false); //attempt to stop loops
		var response = request.execute();
		var responseBody = response.getBody();
		var json = responseBody;
		var obj = JSON.parse(json); //define JSON parsing for the response JSON file to decode
	

 

 

Mark it as helpful and solution proposed if it serves your purpose.

Thanks,

Anand


 

View solution in original post

5 REPLIES 5

Anubhav24
Mega Sage
Mega Sage

Hi @ nandika 1 

You can refer to below thread , I have used it before to send attachments through REST API

 

https://www.servicenow.com/community/developer-forum/send-attachments-as-part-of-rest-message/m-p/13...