Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Downloading file from ServiceNow using Rest API

DKS
Kilo Explorer

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

6 REPLIES 6

sachin_namjoshi
Kilo Patron
Kilo Patron

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

Jace Benson
Mega Sage

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='


find_real_file.png

And it then is able to be opened.

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

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