python 请求错误检查-mk API
python requests error check-mk API
我正在尝试通过 Python 请求向我们的 check-mk Web API 添加字典数据,但我一直收到缺少键的错误:
{"result": "Check_MK exception: Missing required key(s): aux_tags, tag_groups", "result_code": 1}
这是我的代码:
import json
import requests
params_get = (
('action', 'get_hosttags'),
('_username', 'user'),
('_secret', 'secret'),
('request_format', 'json'),
)
params_set = (
('action', 'set_hosttags'),
('_username', 'user'),
('_secret', 'secret'),
('request_format', 'json'),
)
url = 'http://monitoringtest.local.domain/test/check_mk/webapi.py'
tags = ['tag1', 'tag2']
response_data = requests.get(url, params=params_get)
data = response_data.json()
new_data_tags = data['result']['tag_groups']
new_data = data['result']
# new_tags = []
for tag in tags:
new_data['aux_tags'].append({'id': tag, 'title': 'tags in datacenter'})
# new_tags.extend([{'aux_tags': [], 'id': tag, 'title': tag.upper() + ' Tag'}])
# all_tags = new_data_tags.extend([{'tags': new_tags, 'id': 'group1', 'title': 'tags in datacenter'}])
json.dump(data['result'], open("new_file", "w"))
response_data_new = requests.get(url, params=params_set, json=json.dumps(data['result']))
# response_data_new = requests.put(url, params=params_set)
# requests.post(url, params=params_set)
print(response_data_new.text)
# print(data['result'])
# json.dump(data['result'], open("new_file", "w"))
当我使用 curl 时,一切正常并显示成功消息:
{"result": null, "result_code": 0}
您知道导致错误的原因吗?
谢谢
我发现了错误,只是没有集中注意力。 Data变量包含两个键,开头也是result
,结尾是result_code
,需要截断。我只需要按如下方式修改响应,并通过 POST:
发送数据
resp = requests.post(url, params=params_set, data={'request': json.dumps(data['result'])})
感谢@DeepSpace
我正在尝试通过 Python 请求向我们的 check-mk Web API 添加字典数据,但我一直收到缺少键的错误:
{"result": "Check_MK exception: Missing required key(s): aux_tags, tag_groups", "result_code": 1}
这是我的代码:
import json
import requests
params_get = (
('action', 'get_hosttags'),
('_username', 'user'),
('_secret', 'secret'),
('request_format', 'json'),
)
params_set = (
('action', 'set_hosttags'),
('_username', 'user'),
('_secret', 'secret'),
('request_format', 'json'),
)
url = 'http://monitoringtest.local.domain/test/check_mk/webapi.py'
tags = ['tag1', 'tag2']
response_data = requests.get(url, params=params_get)
data = response_data.json()
new_data_tags = data['result']['tag_groups']
new_data = data['result']
# new_tags = []
for tag in tags:
new_data['aux_tags'].append({'id': tag, 'title': 'tags in datacenter'})
# new_tags.extend([{'aux_tags': [], 'id': tag, 'title': tag.upper() + ' Tag'}])
# all_tags = new_data_tags.extend([{'tags': new_tags, 'id': 'group1', 'title': 'tags in datacenter'}])
json.dump(data['result'], open("new_file", "w"))
response_data_new = requests.get(url, params=params_set, json=json.dumps(data['result']))
# response_data_new = requests.put(url, params=params_set)
# requests.post(url, params=params_set)
print(response_data_new.text)
# print(data['result'])
# json.dump(data['result'], open("new_file", "w"))
当我使用 curl 时,一切正常并显示成功消息:
{"result": null, "result_code": 0}
您知道导致错误的原因吗? 谢谢
我发现了错误,只是没有集中注意力。 Data变量包含两个键,开头也是result
,结尾是result_code
,需要截断。我只需要按如下方式修改响应,并通过 POST:
resp = requests.post(url, params=params_set, data={'request': json.dumps(data['result'])})
感谢@DeepSpace