Hi I’ve a question about POST action on API.
To add or delete an host no problem but I have one with POST.
What I’m trying to do:
{code}
url = "https://localost:5665/v1/objects/hosts/"
request_url = url+server_hostname
headers = {
'Accept': 'application/json',
'X-HTTP-Method-Override': 'POST'
}
data = {
"filter": "true",
"templates": service,
"attrs": {
"address":server_ip
}
}
r = requests.post(request_url,
headers=headers,
verify=True,
auth=(api_login, api_password),)
and the error:
{“error”:400.0,“status”:“Invalid type for ‘attrs’ attribute specified. Dictionary type is required.”}
I’m trying to change many things but with no success for the moment.
Thanks for help
Alex
You can try adjusting your filter object to look at the address instead of using an attrs dictionary nested in. Try this in idle as a get request and see if you get what you’re looking for, then apply it to the post action you’re trying to take. If I’m totally wrong right now I’ll help debug, this is crudely leveraged from the first script of mine I found. Also definitely attempt anything destructive in a test environment first.
data = {
'type': 'Host',
'filter': 'host.address == "{0}"'.format(server_ip)
}
Thanks for answer
So I replaced
data = {
"filter": "true",
"templates": service,
"attrs": {
"address":server_ip
}
}
by:
data = {
"type": "Host",
"filter": 'host.address == "{0}"'.format(server_ip)
}
But always the same error:
{“error”:400.0,“status”:“Invalid type for ‘attrs’ attribute specified. Dictionary type is required.”}
What version of Icinga are you using? I tried the following in the Python shell and get an expected result. I’m using 2.11.3 in a qa environment.
>>> import requests,json
>>> icinga_headers = {
'Accept':'application/json',
'X-HTTP-Method-Override': 'GET'
}
>>> icinga_query = {
'type': 'Host',
'filter': 'host.address == "127.0.0.1"',
}
>>> r = requests.get('https://127.0.0.1:5665/v1/objects/hosts',headers=icinga_headers,data=json.dumps(icinga_query),auth=('username','password'),verify=False)
>>> r.json()
I should probably get a little more clarification actually, you said you had no problem to add or delete a host, so I figured you were trying to query one here for a different post action (hence advising filter). What action are you actually trying to take?
In fact with my request I want to update ip information for my server.
when I do PUT action to add or DELETE to delete it works
‘X-HTTP-Method-Override’: ‘PUT’
‘X-HTTP-Method-Override’: ‘DELETE’
Here with my request I just want to update some informations on an existing host.
For my version:
icinga2 - The Icinga 2 network monitoring daemon (version: r2.11.2-1)
Thanks for your help
curl -k -s -u admin:$PASSWORD_API_ADMIN -H ‘Accept: application/json’ -X POST ‘https://localhost:5665/v1/objects/hosts/my_server’ -d ‘{ “attrs”: { “address”: “192.168.1.2”} }’
works
“status”:"Attributes updated.
So it’s really my request on script which is not correct…
ahhhh I’m stupid
I missed to pass data=json.dumps(data))
on my request…
With that necessarily it works…
Thanks for your time !