gpt4 book ai didi

python - AWS EC2 中的 Python 脚本如何与 ServiceNow REST API 通信

转载 作者:行者123 更新时间:2023-11-28 17:58:22 24 4
gpt4 key购买 nike

我正在尝试学习如何通过 REST API 通知 ServiceNow 我使用 Python 脚本(在 AWS EC2 Windows Server 2012 中)更新了 MySQL (AWS RDS) 中的记录,以便它获取更新后的记录。我应该学习哪些特定的 Python 库/模块才能让我朝着正确的方向发展?

目前我的 Python 脚本和 MySQL RDS 通信正常。

我仍处于尝试更好地了解 REST API 和 AWS EC2 的阶段。

如果能分享任何其他 AWS、ServiceNow 或 Python 相关信息,我们将不胜感激。

最佳答案

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 中的 REST API Explorer 对于构建和测试查询非常有用。它甚至生成示例代码。您可以在导航器中搜索 REST API Explorer 来找到它。

另一种选择是创建一个 Scripted REST API在 ServiceNow 中创建一个自定义 URL,您可以点击该 URL 来实现通知。如果您不需要保留数据而只想收到通知,这很好。

关于python - AWS EC2 中的 Python 脚本如何与 ServiceNow REST API 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56975402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com