【发布时间】:2020-10-27 21:35:44
【问题描述】:
正在尝试使用 requests 模块将 shell curl 重写为 python 脚本...
我的 shell 脚本:
#!/bin/bash
ip=$1
url=https://xxx.xx.xx.com
secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
expand=computerStatus
curl --silent -X POST "$url/api/computers/search?expand=$expand" -H "Content-Type: application/json" -H "api-secret-key: $secret" -H "api-version: v1" -d '{"maxItems": 10,"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": "'"$ip"'"}]}' -k > $file
以上在 bash 中运行良好。
我正在尝试转换为类似的 python 等效项
我尝试了什么
import json
import requests
import sys
ip = sys.argv[1]
sys_info = []
url=https://xxx.xx.xx.com
secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
expand=computerStatus
headers = {
'Content-Type': 'application/json',
'api-secret-key': secret,
'api-version': 'v1',
}
params = (
('expand', 'expand'),
)
data = '{"maxItems": 10,"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": ip}]}'
response = requests.post('https://url/api/computers/search?expand=expand', headers=headers, params=params, data=data)
print(response)
<Response [400]>
我收到 400 响应.. 不确定我在语法中遗漏了哪里...
【问题讨论】:
标签: python api curl python-requests http-status-code-400