I had a friend ask me the other day if we could submit attachments through ServiceNow via a RESTful interface rather than through the documented SOAP endpoint. I had to ponder it for a minute before I realized that we could definitely connect to the AttachmentCreator web service via REST just as we do with the SOAP protocol. Please note, however, that as with any of the REST API in ServiceNow, your instance will need to have the “JSON Web Service” plugin enabled.
I ran a quick test on a “ServiceNow” logo and attached it to one of my existing incidents in the following manner:
Step 1:
Convert the file to Base64 format. Depending on your programming language, there should be libraries that do this for you. Base64 encoding allows you to take a binary object and represent it as a string.
With this logo file, the base64 string representation is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | R0lGODlh4AAsANUgAIuMjNjY2LGysumRlfX19W5vb8XFxXd4eN5aYZ6fn+zs7IGCgs7Pz/zx8qip qffW1+Li4uaEifTIyuR2e9YxOdk/RpWVleFobu6ssPG6vbu7u9tMVPnj5eyeotMjLGRlZf///wAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAACAALAAAAADgACwAAAb/QJBw SCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8Tq/b7/i8fs/v+/+AgYJl DA4Ah4cCAUwKGgmIAA4MSAECBkMGFoiXIAIACQRNEI+TBI8CSRCekAKTTAQGj4iSSRkDGA1EGREI vRO4UBEeHhNKDR0IEw9VGb0YUwYFH9PU1AecRgoJ1dwHrkTUAgQL3B+K1ABNB9Sm1ItFDOvl0wca SQQC0vMFqEUDwx4q5MpQAaBBDwOcPDD47AgHCgApcJiSweAGELx8RWhQIcORbfO4JTDCQF/IaSPB 0SNXDoCCau+SGLAGAoA7IgQsnKy2IFQR/wjydgLwKeQfwAkTDirdkGtJB4MJj0g4GGHKhoMgOEzY 8ACBB2FRiQjgVuAQy2rYQMxsGZRaS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ATdIxrF9EyY5Fr3KfvbTZTzqyQwQADGYRmjnhC+IyAOYjdSAs8Y B4L1DUBWARECniS3Pvt84SwBuNc0AHUPB2CDf1GjRl42BKIvXWg5GSrClh5lnKAUEAkJAFQDjYC4 DUouYzk6iMTa5iKcVTBhBnmWE0BGNRzeZEMFwJ4R6MIJFCqhbyL8QIa+NMMJKSFTi3ghcpYgIe3s EDR22pYHLgBENbIKW4uRGAT5JIVyTf8thgPyHwhYFsUkbOh7VlSCcoh1hC91ohqgcCI1XNPFR3ls CGJM2RmJgDg4JgxWcUJVBImAQcvdaTFGJFc1rtGggNFECC9TGk6g4zTUPYFTUxLgGIVgGFISgQEe +h4IuocS7OGyGgKa5BAqmbulaHIYoYScYpKZtqBNYW4AsIAh/nKdoz3KLOXoUiCVQJhDIcGQAzLM NBZwCKQtEJbXLCdZmiPMyykOBGrszouM8DMf8UgxQHyF3ebRDyKwLi5dAsE2k/DPD5BQlrDDyT67 IUV0huQATmonCIipTHkiEwk+HIbCiDBBh1lBA50b5SPBtNBDHXSgSGCZFpEARiNooJuV3HAAHhVQ Un4MB2IbPYKLxjWfYzosc0ZYH2WMgCoPWqEQADDJJ0YqGEPooyy0MIKE+vkE2FwjFesYChJicZYF WAB3ldIAAILyCbAW4TwVkNgRCAKfZBLhAaSRVQUQMAC1YnRPFJgAEB+AV70O4q+ADaxgB0vYwhr2 sIhNrGIXy9jGOvaxkI2sZCdL2cpa9rKYzewQggAAOw== |
Step 2:
Prepare your REST post in the following manner:
POST URL
https://[YOURINSTANCE].service-now.com/ecc_queue.do?JSON&sysparm_action=insert
POST Body
{
“agent”:”AttachmentCreator”,
“topic”:”AttachmentCreator”,
“name”:”[FILENAME]:[MIME_TYPE_FOR_FILENAME]”,
“source”:”[TABLE_NAME]:[SYS_ID_OF_RECORD_TO_ATTACH_TO]”,
“payload”:”[INSERT_BASE64_STRING_HERE]”
}
For my example, the POST Body looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | { "agent":"AttachmentCreator", "topic":"AttachmentCreator", "name":"now.gif:application/gif", "source":"incident:9c573169c611228700193229fff72400", "payload":"R0lGODlh4AAsANUgAIuMjNjY2LGysumRlfX19W5vb8XFxXd4eN5aYZ6fn+zs7IGCgs7Pz/zx8qip qffW1+Li4uaEifTIyuR2e9YxOdk/RpWVleFobu6ssPG6vbu7u9tMVPnj5eyeotMjLGRlZf///wAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAACAALAAAAADgACwAAAb/QJBw SCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8Tq/b7/i8fs/v+/+AgYJl DA4Ah4cCAUwKGgmIAA4MSAECBkMGFoiXIAIACQRNEI+TBI8CSRCekAKTTAQGj4iSSRkDGA1EGREI vRO4UBEeHhNKDR0IEw9VGb0YUwYFH9PU1AecRgoJ1dwHrkTUAgQL3B+K1ABNB9Sm1ItFDOvl0wca SQQC0vMFqEUDwx4q5MpQAaBBDwOcPDD47AgHCgApcJiSweAGELx8RWhQIcORbfO4JTDCQF/IaSPB 0SNXDoCCau+SGLAGAoA7IgQsnKy2IFQR/wjydgLwKeQfwAkTDirdkGtJB4MJj0g4GGHKhoMgOEzY 8ACBB2FRiQjgVuAQy2rYQMxsGZRaSiE7p6XTiXKJzWkOat4UMm5eW2o9iUAwCfgsYKJGlSoGeMEI BwkShiRGOOSBhIkgGhy8KBVy0yIHG4MYgAAEgqsUwvI1aQECznzohgQQ6ZpvtGr24HYzQEDB2EsM qhHNVk2B3mkx707jZ1yIAgNtLQxRQBjAN7Vt8xZdzH1Y5MoQPSDINTlq0mENvRr8LORBeA8UlhGZ arBD0dIIMCBTrTb2kb4fSAcCAUEVEBNOZ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f8thgPyHwhYFsUkbOh7VlSCcoh1hC91ohqgcCI1XNPFR3ls CGJM2RmJgDg4JgxWcUJVBImAQcvdaTFGJFc1rtGggNFECC9TGk6g4zTUPYFTUxLgGIVgGFISgQEe +h4IuocS7OGyGgKa5BAqmbulaHIYoYScYpKZtqBNYW4AsIAh/nKdoz3KLOXoUiCVQJhDIcGQAzLM NBZwCKQtEJbXLCdZmiPMyykOBGrszouM8DMf8UgxQHyF3ebRDyKwLi5dAsE2k/DPD5BQlrDDyT67 IUV0huQATmonCIipTHkiEwk+HIbCiDBBh1lBA50b5SPBtNBDHXSgSGCZFpEARiNooJuV3HAAHhVQ Un4MB2IbPYKLxjWfYzosc0ZYH2WMgCoPWqEQADDJJ0YqGEPooyy0MIKE+vkE2FwjFesYChJicZYF WAB3ldIAAILyCbAW4TwVkNgRCAKfZBLhAaSRVQUQMAC1YnRPFJgAEB+AV70O4q+ADaxgB0vYwhr2 sIhNrGIXy9jGOvaxkI2sZCdL2cpa9rKYzewQggAAOw==" } |
The Response
After posting, I got the following JSON string back as the response to my request:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | { "records":[ { "agent":"AttachmentCreator", "agent_correlator":"null", "error_string":"null", "from_host":"null", "from_sys_id":"null", "name":"now.gif:application/gif", "payload":"Attachment created and attached to 'incident' record with sys_id: '9c573169c611228700193229fff72400'", "processed":"", "queue":"input", "response_to":"null", "sequence":"13f5759f6970000001", "source":"incident:9c573169c611228700193229fff72400", "state":"processed", "sys_created_by":"admin", "sys_created_on":"2013-06-18 12:54:41", "sys_id":"899ed425b8428100f8e677b6d50cd416", "sys_mod_count":"0", "sys_updated_by":"admin", "sys_updated_on":"2013-06-18 12:54:41", "topic":"AttachmentCreator" } ] } |
The Final Result
I was able to verify that the corresponding incident record was updated with the file attachment.
Have you tried the oposite way? Sending an Attachment from ServiceNow to some other system via REST?
Marcelo, good question. Yes, you can send attachment data via REST as well. I would refer you to one of my blog posts about SOAP: https://john-james-andersen.com/blog/service-now/sending-out-servicenow-attachments-via-soap.html
Of course, it depends on how your provided web service needs the attachment data, but you would follow the same steps, except instead of sending the data via SOAP at the end of the script, you would send it with our REST Message library instead.
Thanks John! I’ll try that out 😉
When I attempt this post, I do produce a record in the ECC Queue, however, all fields are blank.
Lesser privileged accounts get an “Insufficient rights” message, so I know the user account I am using is permissioned. Are there additional permissions I need to consider?
Below is the JSON record I get back. Note ‘topic’ and ‘source’ are blank.
@Joanne, I first try these with Admin roles just to make sure the permissions are not an issue. Once I have it all working, I then narrow down the user roles to be what I need them to actually be. Without seeing your original request parameters and body, I can’t really comment on the response.
When I tried to send attachement to https://.service-now.com/ecc_queue.do?JSON&sysparm_action=insert, I am getting following error.
{“error”:”Insufficient rights to insert record: ecc_queue”}
Please advice how to resolve this issue.
How can we make the code dynamic such that it is able to send attachments of any format(xml,pdf,txt etc)
This will not work as coded!
JSON needs to be followed by V2
Thanks for the great site!
As mention by Richard, it depends on the JSON plugin installed.
On recent version use as POST URL
https://[YOURINSTANCE].service-now.com/ecc_queue.do?JSONv2&sysparm_action=insert
Regarding right to write in ecc_queue, role rest_service itslef is not sufficient. you need to add ACL on ecc_queue/record/create with role rest_service for the call to succeed.
Does this works fine form file greater then 5MB in size.
@rohant, in all actuality, i would recommend that you take a look at the REST Attachment API released by ServiceNow last year. It will support many more ways of posting attachments into ServiceNow and will provide better flexibility with file sizes. Please see the following docs:
1) REST Attachment API Doc page
https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/integrate/inbound-rest/concept/c_AttachmentAPI.html
2) Developer API documentation for the REST Attachment API
https://developer.servicenow.com/app.do#!/rest_api_doc?v=istanbul&id=c_AttachmentAPI
Hi John,
So can we synchronize data with salesforce if any changes happened at the serviceNow Incident?
I mean can it be bidirectional?
Thank You
Hello John,
REST API Explorer provides a place to upload the attachments (Binary or Multipart). What if we want to pick the attachment from a local folder (in case of bulk uploads). Can you please advise a way to achieve this.
Hi John,
When I tried to attach a file from a specified folder, its throwing error.
Also, the outlook mails cant be opened using the above method
does it works for multiple attachment
Hi John,
Can we send multiple attachments to the third party when the incident record is inserted in the incident table in ServiceNow.
Does anyone got solution for attaching multiple attachments at a time