1

I am trying to add dictionary data to our check-mk Web API wit Python requests, but I keep getting an Error of missing keys:

{"result": "Check_MK exception: Missing required key(s): aux_tags, tag_groups", "result_code": 1}

Here is my code:

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"))

When I use curl every thing works well with a success message:

{"result": null, "result_code": 0}

Do you have any idea what causes the error? Thanks

Max
  • 543
  • 6
  • 17
  • 1
    "Missing required key(s): aux_tags, tag_groups"" Have you tried adding the missing keys to your request? Seems like you attempt to add `aux_tags` to `new_data`, but `new_data` is not used in the actual request – DeepSpace Aug 29 '18 at 10:15
  • The keys are not missing, they in the file `new_file`, when I copy the content and send it to the API with curl, it works. It seems to me like a parsing problem. – Max Aug 29 '18 at 10:20
  • 1
    The file has nothing to do with the request, you open the file after sending the request. The curl request must be doing something else. It will be more beneficial if you add the curl command you are using since it needs to be imitated by `request.post`. – DeepSpace Aug 29 '18 at 10:22
  • I updated the code, POST is not implemented, only GET and PUT. I also open the file before sending the requests to see what is in there, the same issue. with curl I just use `curl "http://monitoringtest.local.domain/test/check_mk/webapi.py?action=set_hosttags&_username=user&_secret=secret" -d 'request{data content}"` – Max Aug 29 '18 at 10:32
  • I tried to force json with `json.dumps(data['result']))` without any success – Max Aug 29 '18 at 10:47
  • I had a typo in the curl command, here is the correct command: `curl "http://monitoringtest.local.domain/test/check_mk/webapi.py?action=set_hosttags&_username=user&_secret=secret" -d 'request{data content}'` – Max Aug 29 '18 at 10:56

1 Answers1

0

I found the mistake, it was just not focused. The Data variable contains two keys that are sent as well result at the beginning and result_code at the end, which need to be truncated. I just had to modify the response as follows, and sending the data wit POST:

resp = requests.post(url, params=params_set, data={'request': json.dumps(data['result'])})

Thanks @DeepSpace

Max
  • 543
  • 6
  • 17