Downloading file from ServiceNow using Rest API
This content was last updated in 2019. It may still be useful, but could contain outdated information. Please confirm its accuracy before relying on it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2019 07:14 PM
Hi,
First of all, I have gone through the documentation on Attachment API. I am able to get the repsonse from the API and also the Binary response(as mentioned in the doc) for the attachment. I dont quite understand how to decode this reponse and create back the original file that is there as attachment in SN.
Actually I have created a script in python and my response has characters like this - ��]�������VX�M��꥟�����2I
The same I am getting from postman as well. Please help how can I create a actual file from this.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2019 08:36 PM
Please below powershell code for downloading file from service now
http://chen.about-powershell.com/2018/08/download-servicenow-incident-attachment-using-powershell/
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2019 09:27 PM
You shouldn't have to do any conversion of the data. Just drop it into a file; (this is a pdi and nothing to hide feel free to try on your own with that authorization)
curl --request GET \
--url https://dev69973.service-now.com/api/now/attachment/11f1e367db2423002a66364e9d9619bd/file \
--header 'authorization: Basic c2xhY2s6c2xhY2s='
And it then is able to be opened.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2019 11:32 PM
I assume that you are suggesting to use the "download_link" instead of the binary response. For some reason, the download_link is not working in my browser as well as in python even though I supply the correct credentials when it asks for. So now my only solution is to decode/encode the response and create the file. Can you suggest me how to do that?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2019 12:39 PM
I'm not using the download_link.
This is the binary download endpoint as documented here.
I provided a curl command with credientials that work, you should be able to do the same in python.
Here's what my rest editor says a working python example would be;
import http.client
conn = http.client.HTTPSConnection("dev69973.service-now.com")
payload = ""
headers = {
'authorization': "Basic c2xhY2s6c2xhY2s="
}
conn.request("GET", "/api/now/attachment/11f1e367db2423002a66364e9d9619bd/file", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))//yo'd have to write this to something.png