我正在尝试将字典数据添加到我们的check-mk Web API和Python请求中,但是我不断收到缺少键的错误消息:
{"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}
您是否知道导致错误的原因? 谢谢
答案 0 :(得分:0)
我发现了错误,只是没有集中精力。 Data变量包含两个密钥,它们分别在开头CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
和结尾result
处发送,并且需要将其截断。我只需要按如下方式修改响应,然后通过POST发送数据:
result_code
感谢@DeepSpace