Table API Python examples

Examples that demonstrate how to use the Table API with the Python language.

Example URLs

In the examples, replace myinstance.service-now.com with the URL of your instance.

GET

#Need to install requests package for python
 #sudo easy_install requests
 import requests
 
 # Set the request parameters
 url = 'https://myinstance.service-now.com/api/now/table/incident?sysparm_limit=10'
 user = 'admin'
 pwd = 'admin'
 
 # Set proper headers
 headers = {"Accept":"application/json"}
 
 # Do the HTTP request
 response = requests.get(url, auth=(user, pwd), headers=headers )
 
 # 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
 print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())
 print('Cookies', response.cookies)

POST

#Need to install requests package for python
 #sudo easy_install requests
 import requests
 
 # Set the request parameters
 url = 'https://myinstance.service-now.com/api/now/table/incident'
 user = 'admin'
 pwd = 'admin'
 
 # Set proper headers
 headers = {"Content-Type":"application/json","Accept":"application/json"}
 
 # Do the HTTP request
 response = requests.post(url, auth=(user, pwd), headers=headers ,data='{"short_description":"Test"}')
 
 # Check for HTTP codes other than 200
 if response.status_code != 201: 
     print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
     exit()
 
 # Decode the JSON response into a dictionary and use the data
 
 print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())

PUT

 #Need to install requests package for python
 #sudo easy_install requests
 import requests
 
 # Set the request parameters
 url = 'https://myinstance.service-now.com/api/now/table/incident/7ca3ef46d4b61100a92e188eafa1ddcd'
 user = 'admin'
 pwd = 'admin'
 
 # Set proper headers
 headers = {"Content-Type":"application/json","Accept":"application/json"}
 
 # Do the HTTP request
 response = requests.put(url, auth=(user, pwd), headers=headers ,data='{"short_description":"Test Update"}')
 
 # 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
 print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())

PATCH

#Need to install requests package for python
 #sudo easy_install requests
 import requests
 
 # Set the request parameters
 url = 'https://myinstance.service-now.com/api/now/table/incident/7ca3ef46d4b61100a92e188eafa1ddcd'
 user = 'admin'
 pwd = 'admin'
 
 # Set proper headers
 headers = {"Content-Type":"application/json","Accept":"application/json"}
 
 # Do the HTTP request
 response = requests.patch(url, auth=(user, pwd), headers=headers ,data='{"short_description":"Test update Patch"}')
 
 # 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
 print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())

DELETE

#Need to install requests package for python
 #sudo easy_install requests
 import requests
 
 # Set the request parameters
 url = 'https://myinstance.service-now.com/api/now/table/incident/b684ebc6d4b61100a92e188eafa1dd65'
 user = 'admin'
 pwd = 'admin'
 
 # Set proper headers
 headers = {"Content-Type":"application/json","Accept":"application/json"}
 
 # Do the HTTP request
 response = requests.delete(url, auth=(user, pwd), headers=headers)
 
 # Check for HTTP codes other than 200
 if response.status_code != 204: 
     print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
     exit()
 
 # Decode the JSON response into a dictionary and use the data
 print('Status:',response.status_code,'Headers:',response.headers)