python - 我需要帮助将此 REST API Curl 命令转换为 Python 请求

标签 python rest curl python-requests rt

我是这里的新人,说实话,对所有编码都很陌生。

我正在尝试创建一个 Pyton 脚本来使用 REST API 从请求跟踪器 Assets 数据库中搜索项目。

到目前为止我得到了这个 Curl 命令:

curl    -X POST \
-H "Content-Type: application/json" \
-d '[{ "field" : "Owner", "operator" : "LIKE", "value" : "NAME" },{"field":"Catalog", "value":"1"}]' \
-H 'Authorization: token MY_TOKEN' \
'https://RT_URL/rt/REST/2.0/assets'

它会返回一个漂亮的 JSON,其中包含来自 RT_URL 的结果,其中所有者使用 token MY_TOKEN 来匹配 NAME。

但我不知道如何用 Python 编写这个代码。我有一个使用 requests 库来获取的脚本,它使用简单的 URL 请求,但我不知道如何实现搜索字段。

我到处寻找示例,但无法让它工作。我没有找到任何有关如何使用 token 在请求中进行身份验证的信息。

无论如何,提前感谢您的回复:)

最佳答案

试试这个代码

import requests

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'token TOKEN',
}

data = '[{ "field" : "value"}] ......'

response = requests.post('YOUR_URL', headers=headers, data=data)

关于python - 我需要帮助将此 REST API Curl 命令转换为 Python 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53707602/

相关文章:

python - 饼图 Python : how to put the labels in a legend box

python - 无法打开需求文件 : [Errno 2] No such file or directory: 'requirements.txt'

python - 写入 Excel 时“Unsparsify” Pandas 多索引

php - 如何使用 CURL 在 Facebook 页面上发布帖子而不在 PHP 中使用 SDK

bash - 即使状态代码为 400,如何从 curl 请求输出到文件?

python - 我的名字中有两个字母数量相等,如何打印这两个字母及其数量

python - Django Serializer POST 数据未完全传递给序列化器

java - org.springframework.web.client.RestTemplate 预计被声明为 @Bean 和普通 @Autowired 抛出错误

rest - 带有 REST API 空回复的 OTRS Webservice curl

tomcat - 我的 docker 容器 kong 以 "An invalid response was received from the upstream server"响应