Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bryanbarnard/87371cbb582601392a745aca3cd1038d to your computer and use it in GitHub Desktop.
Save bryanbarnard/87371cbb582601392a745aca3cd1038d to your computer and use it in GitHub Desktop.
ServiceNow REST Attachment API Example Python Example using /now/attachment/file endpoint to post (upload) a file as an attachment to an incident as binary.
#Need to install requests package for python
#easy_install requests
import requests
# Request Docs: http://docs.python-requests.org/en/master/user/quickstart/
# Set the request parameters
url = 'https://bbarnsc1.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=81f8915bdb6ba20028927416bf961971&file_name=issue_screenshot'
# url = 'https://fejr5sb8ead6.runscope.net/api/now/attachment/file?table_name=incident&table_sys_id=81f8915bdb6ba20028927416bf961971&file_name=issue_screenshot'
# specify files to send as binary
data = open('issue_screenshot.jpg', 'rb').read()
# Eg. User name="admin", Password="admin" for this code sample.
user = 'xxxx'
pwd = 'xxxxxx'
# Set proper headers
headers = {"Content-Type":"image/jpg","Accept":"application/json"}
# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers, data=data)
# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)
@alamsohelrana
Copy link

Getting error : 'error': {'message': ' is not an authorized file extension', 'detail': None}, 'status': 'failure'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment