How to Insert Attachment using Binary/Base64 format

Rohit Kumar
Giga Expert

Hi All,

I wanted to utilize REST Attachment API for inserting Attachment on Incident. If I try REST API Explorer using Attachment API, I need to choose file stored on my local (which is not required) and then it inserts the record in attachment.

I am fetching the file from external system using REST and then it returns in binary format of file in response. Now, the same binary format I wanted to utilize in creating the attachment in ServiceNow. I can even try to convert the file to base64 but attachment API works in binary format itself.

Can somebody please let me know what should be the request format for the REST attachment API for binary OR base64 (Note: REST API Explorer let me to choose file but it is already there in my code variable)

Also, I don't want to utilize the following link. "Generate Attachments in ServiceNow via REST | John Andersen " & AttachmentCreator SOAP Web Service - ServiceNow Wiki as they utilize ecc_queue.

I simply want to use Attachment API which will be very easy if implemented.

Below is the format of file in response from external system. Now, I want to convert this format as file in attachment table.

find_real_file.png

1 ACCEPTED SOLUTION

Rohit Kumar
Giga Expert

This got resolved via various hit and try methods of ServiceNow attachments.



Inserting Attachments in ServiceNow from JIRA without MID-Server using REST


View solution in original post

4 REPLIES 4

gauravchoudhury
Tera Guru

Hi Rohit,



The REST Attachment API accepts attachments in binary or multi-part format.



Refer to this:



Attachment API - POST /now/attachment/upload



Binary: Attachment API - POST /now/attachment/file


Multipart : Attachment API - POST /now/attachment/upload



There is a SOAP endpoint that accepts Base 64.


Sending an attachment to ServiceNow via Web Services - ServiceNow Guru


Understood Attachment API was available since Helsinki and supports only Binary or Multiple at this stage.



There is another option if you prefer to upload via base64str with REST API.


Similar to SOAP Web Services approach where payloads are sent to ECC Queue, you may try to upload it via REST TABLE API.


However, please ensure business rules "AttachmentCreatorSensor" is available before you proceed.



find_real_file.png



If you are not using admin user, you will need to grant additional role (to Create ACL) to permit this upload to ecc_queue table.



End point: https://{your_instance}.service-now.com/api/now/table/ecc_queue


Method: POST


Sample Payload:


{


"agent":"AttachmentCreator",


"topic":"AttachmentCreator",


"name":"johnny_chong.txt:text/plain",


"source":"incident:7872832f4f814300bc924c111310c75d",


"payload":"SGksDQpDb25ncmF0dWxhdGlvbnMuDQpJZiB5b3UgY2FuIHNlZSB0aGlzIGluIHBsYWluIEVuZ2xpc2gsIHRoYXQncyBtZWFuIHlvdSBnb3QgaXQuDQoNClJlZ2FyZHMsDQoNCkpvaG5ueSBDaG9uZywNCmh0dHBzOi8vd3d3LnRjbG91ZGNvbnN1bHRpbmcuY29tLmF1DQo="


}


Rohit Kumar
Giga Expert

Hi Gaurav,



These links I have already gone through. I don't know how to run cURL from ServiceNow and the cURL/powershell requires to post attachment by fetching the file from the local system.


The file I am fetching from external system in Binary or Base64 format and just need to insert the same on attachment table.



Thanks


Rohit


Rohit Kumar
Giga Expert

This got resolved via various hit and try methods of ServiceNow attachments.



Inserting Attachments in ServiceNow from JIRA without MID-Server using REST