How do attach file with incident in service now using rest api?

rameshsharma
Kilo Explorer

How do attach file with incident using rest api like jsonv2/table api.? Please help

4 REPLIES 4

Tim Woodruff
Mega Guru

Hello,



A little more information would be useful, such as what version of ServiceNow you're on and some more details about what you're trying to do, but I'll do my best to help, and hopefully it's relevant to what you're looking for. 🙂


Assuming you're on one of the latest versions, there is actually a REST "Attachment API" built-in.



Here is an example snippet from my instance:



var restRequestBody = "YOUR_BINARY_BLOB_HERE";



var restMsg = new XMLHttpRequest();
restMsg.open("post", "https://YOUR_INSTANCE.service-now.com/api/now/attachment/file?table_name=" +


    "YOUR_TABLE_NAME" +


    "&table_sys_id=" +


    "YOUR_TABLE_SYS_ID" +


    "&file_name=" +


    "YOUR_FILE_NAME");



restMsg.setRequestHeader('Accept', 'application/json');
restMsg.setRequestHeader('Authorization', 'Basic ' + btoa('USERNAME' + ':' + 'PASSWORD'));
//You can optionally specify another header for Content-Type and Content-Length



restMsg.onreadystatechange = function() {


    //The inline anon function inherits the state of the XMLHttpRequest object; hence 'this.<readyState/DONE>'.
    if (this.readyState == this.DONE) {


    document.getElementById("response").innerHTML = this.status + this.response;
    }


};



restMsg.send(restRequestBody);



client.send(requestBody);



Sending this, will attach whatever file's binary data you have in the requestBody variable. There are a multitude of ways to get a file as a binary blob or string. Here's a fairly comprehensive article, and here's a StackOverflow post with two good solutions if you need them.


Hi Timothy Woodruff,,


Thanks for your reply.



I am using helsinki version of service now and trying to attach a file with an incident using service api like jsonv2/table api.



Thanks,


Ramesh Sharma


My post described how to do that. Was there something I missed?


chirag_bagdai
ServiceNow Employee
ServiceNow Employee

Hi Ramesh,



Please find below reference link which can be helpful



Attachment API