3

I am trying to learn how to inform ServiceNow via REST API that I updated a record in MySQL (AWS RDS) using a Python script (in AWS EC2 Windows Server 2012) for it fetch that updated record. What particular Python libraries / modules should I learn to put me in the right direction?

Currently my Python script and MySQL RDS are communicating just fine.

I am still in the phase of trying to get a better understanding of REST API and AWS EC2.

Any other AWS, ServiceNow, or Python related information that could be share would be greatly appreciated.

criz
  • 273
  • 1
  • 11
  • I am not sure I understand what you are looking for.. You have python code that update a record in MySQL (which happens to be on RDS). Now you want to call ServiceNow and let it know the details of the update. What is the question? – balderman Jul 10 '19 at 17:11
  • See https://stackoverflow.com/questions/17301938/making-a-request-to-a-restful-api-using-python – jarmod Jul 10 '19 at 17:39
  • The fact that you're making the REST request from an EC2 instance is mostly irrelevant. In this case the EC2 instance can be regarded as a normal computer. Just make sure the inbound/outbound traffic rules on the security group associated with the instance allow the requests you're sending through. – Alex Jul 10 '19 at 19:31

1 Answers1

1

The ServiceNow table REST API is very straightforward so inserting a record into an arbitrary table with Python is a breeze. For example:

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://[instance name].service-now.com/api/now/table/[table name]'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request - this is fake data
response = requests.post(url, auth=(user, pwd), headers=headers ,data="[your json string of fields and values]")

# 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)

The REST API Explorer in ServiceNow if very useful for building and testing queries. It even generates sample code. You can search REST API Explorer in the navigator to find it.

Another option is to create a Scripted REST API in ServiceNow to create a custom URL that you can hit to achieve the notification. This is good if you don't need to persist the data and just want to be notified.

mr.freeze
  • 13,731
  • 5
  • 36
  • 42
  • Thank you. I tried this against SNOW and it works on GET and POST. All I need now is know how to automatically trigger get a new ticket to RDS. – criz Jul 12 '19 at 11:23
  • Please upvote or mark as answer if it solved your problem. – mr.freeze Jul 12 '19 at 13:23