awsec2中的Python脚本如何与ServiceNow restapi通信

2024-07-08 11:08:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习如何通过restapi通知ServiceNow,我使用Python脚本(在awsec2windowsserver2012中)在MySQL(awsrds)中更新了一条记录,以便获取更新的记录。我应该学习哪些特定的Python库/模块来引导我正确的方向?在

目前,我的Python脚本和MySQL RDS的通信很好。在

我仍处于尝试更好地理解restapi和awsec2的阶段。在

任何其他可以共享的与AWS、ServiceNow或Python相关的信息都将不胜感激。在


Tags: 模块脚本awsrestapi信息记录mysql方向
1条回答
网友
1楼 · 发布于 2024-07-08 11:08:57

ServiceNow table REST API非常简单,因此使用Python将记录插入任意表中非常简单。例如:

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

ServiceNow中的restapiexplorer对于构建和测试查询非常有用。它甚至生成示例代码。您可以在navigator中搜索restapiexplorer来找到它。在

另一个选择是在ServiceNow中创建一个Scripted REST API,以创建一个自定义URL,您可以点击该URL来获得通知。如果您不需要持久化数据而只想得到通知,那么这是很好的。在

相关问题 更多 >

    热门问题