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'

@eddyvk01
Copy link

eddyvk01 commented May 3, 2024

import requests

file = request.FILES['inputGroupFile']
if file:
file_name = file.name
file_type = file.content_type
logging.info("=============================================")
logging.info(f"File name: {file_name}, File type: {file_type}")

        files = {
            'file': (file_name, file, file_type)
        }

        url = f"https://{SERVICE_NOW_INSTANCENAME}.service-now.com/api/now/attachment/upload"

        # Prepare headers and payload 
        headers = {
            "Accept": "application/json"
        }
        payload = {
            'table_name': 'incident',
            'table_sys_id': incident_sys_id
        }

        # Send the request
        response = requests.post(url, auth=(SERVICE_NOW_USER, SERVICE_NOW_PASSWORD), headers=headers, files=files, data=payload)
        # Check the response
        if response.status_code == 201:
            logging.info("File uploaded successfully.")
            return JsonResponse({
                "messageStatus": "Successful",
                "message": "File attached successfully to the incident",
                "data": response.json()
            })
        else:
            logging.error("Failed to upload file: " + str(response.status_code))
            logging.info(response.json())
            return JsonResponse({
                "messageStatus": "Failed",
                "message": "Failed to attach file to the incident",
                "data": response.json()
            })

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