REST Attachment API corrupting files

Shilbhadra Mukh
Kilo Contributor

While trying to exchange attachments with a third party using a scripted webservice and Attachment API, I came across a peculiar problem.

I created a scripted webservice (as the third party will only communicate over SOAP) which processes requests to add  attachments to and get attachments from our Service Catalog Request Item table (actually from the sys_attachment table obviously) using the Attachment API. In an effort to test my methods I tried to feed the binary (supposedly) output from my 'getAttachment()' method to my 'addAttachment()' method. This should have ideally copied the attachments from one Request Item to another. But in practice, while the attachments are created in the destination Request Item, they are not usable as they are getting corrupted.

Can someone please tell me why the Attachment API cannot create attachments from the very binary data that it provides for existing attachments? Or where I am going wrong?

I know that I can use something like this to get the encoded binary data for the attachments:

var gsa = new GlideSysAttachment();
var encodedBody = GlideStringUtil.base64Encode(gsa.getBytes(gr));

But I don't want to as I don't want to risk using undocumented functions which have the potential to impact my production instance.

I also know that I can use the 'ecc_queue' to create attachments from Base64 encoded binary data. Again this is not recommended by ServiceNow so I do not want to use it.

The following are some of my code samples.

Function to get attachment body:

function getAttachmentBody(attachmentSysId) {
	
	var request = new sn_ws.RESTMessageV2();
	request.setEndpoint('https://instance.service-now.com/api/now/attachment/' + attachmentSysId + '/file');
	request.setHttpMethod('GET');

	var user = 'admin';
	var password = 'admin';

	request.setBasicAuth(user,password);
	request.setRequestHeader("Accept","*/*");

	var response = request.execute();
	
	return response.getBody();
}

 

Function to get attachment meta:

function getAttachmentMeta(attachmentSysId) {
	
	var request = new sn_ws.RESTMessageV2();
	request.setEndpoint('https://instance.service-now.com/api/now/attachment/' + attachmentSysId);
	request.setHttpMethod('GET');
	
	var user = 'admin';
	var password = 'admin';

	request.setBasicAuth(user,password);
	request.setRequestHeader("Accept","application/json");

	var response = request.execute();
	return response.getBody();
}

 

Function to add attachment:

function addAttachment(ritmSysId, fileName, contentType, bodyBinary) {
	
	var request = new sn_ws.RESTMessageV2();
	request.setEndpoint('https://instance.service-now.com/api/now/attachment/file?table_name=sc_req_item&table_sys_id=' + ritmSysId + '&file_name=' + fileName);
	request.setHttpMethod('POST');
	
	var user = 'admin';
	var password = 'admin';
	
	request.setBasicAuth(user,password);
	request.setRequestHeader("Accept","application/json");
	request.setRequestHeader('Content-Type', contentType);
	request.setRequestBody(bodyBinary);
	
	var response = request.execute();
}
4 REPLIES 4

Mike Patel
Tera Sage

Not sure why you are doing REST since  (as the third party will only communicate over SOAP).

There is SOAP method to insert attachment to SN

https://xxxx.service-now.com/ecc_queue.do?SOAP

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ecc="http://www.service-now.com/ecc_queue">
   <soapenv:Header/>
   <soapenv:Body>
      <ecc:insert>
         <ecc:agent>AttachmentCreator</ecc:agent>
         <ecc:topic>AttachmentCreator</ecc:topic>
         <ecc:name>error.jpg:image/jpeg</ecc:name>
         <ecc:source>incident:xxxxxxxxxxxxxxxxxxxxxxx</ecc:source>
         <ecc:payload>ffffffffffffffff</ecc:payload>
      </ecc:insert>
   </soapenv:Body>
</soapenv:Envelope>

Because it is not recommended by ServiceNow (as I have already mentioned in my post). See this:

"Important: The AttachmentCreator SOAP web service is not recommended. Instead, use the REST Attachment API to manage attachments with web services."

Thanks, Looks like I'll have to move from SOAP to REST now.

Shilbhadra Mukh
Kilo Contributor

Because it is not recommended by ServiceNow (as I have already mentioned in my post). See this:

"Important: The AttachmentCreator SOAP web service is not recommended. Instead, use the REST Attachment API to manage attachments with web services."