Director API: PUT request doesn't works (redirected), GET instead works

Hi all,
I have a laboratory icinga2 environment all running as container.

Following the containers version:

Following the Icingaweb2 and it’s modules version:

I have a problem in creating object via API.

GET requests respond correctly:

root@rgserver:/docker-apps$ curl -u api:api -H ‘Accept:application/json’ -X GET ‘http://172.19.0.10:8080/director/host?name=icinga-satellite’ -v

  • About to connect() to 172.19.0.10 port 8080 (#0)
  • Trying 172.19.0.10…
  • Connected to 172.19.0.10 (172.19.0.10) port 8080 (#0)
  • Server auth using Basic with user ‘api’

GET /director/host?name=icinga-satellite HTTP/1.1
Authorization: Basic YXBpOmFwaQ==
User-Agent: curl/7.29.0
Host: 172.19.0.10:8080
Accept:application/json

< HTTP/1.1 200 OK
< Date: Mon, 07 Oct 2024 08:01:20 GMT
< Server: Apache/2.4.56 (Debian)
< Content-Length: 371
< Content-Type: application/json
<
{
“accept_config”: true,
“address”: “icinga-satellite”,
“display_name”: “icinga-satellite”,
“fields”: ,
“has_agent”: true,
“imports”: [
“host-template”
],
“master_should_connect”: true,
“object_name”: “icinga-satellite”,
“object_type”: “object”,
“uuid”: “1df2e5bd-1871-4f70-abca-ff41f6d14e76”,
“zone”: “master”
}

  • Connection #0 to host 172.19.0.10 left intact

PUT requests seem redirect to login page:

root@rgserver:/docker-apps$ curl -u api:api -H’Accept:application/json,charset:utf-8’ -X PUT ‘http://172.19.0.10:8080/director/host’ -d ‘{“object_name”: “prova_host”, “imports”: “host-template”, “display_name”: “prova_host”, “address”: “127.0.0.1”, “zone”: “satellite”}’ -v

  • About to connect() to 172.19.0.10 port 8080 (#0)

  • Trying 172.19.0.10…

  • Connected to 172.19.0.10 (172.19.0.10) port 8080 (#0)

  • Server auth using Basic with user ‘api’

PUT /director/host HTTP/1.1

Authorization: Basic YXBpOmFwaQ==

User-Agent: curl/7.29.0

Host: 172.19.0.10:8080

Accept:application/json,charset:utf-8

Content-Length: 132

Content-Type: application/x-www-form-urlencoded

  • upload completely sent off: 132 out of 132 bytes

< HTTP/1.1 302 Found

< Date: Mon, 07 Oct 2024 08:05:46 GMT

< Server: Apache/2.4.56 (Debian)

< Location: /authentication/login?redirect=director%2Fhost

< Content-Length: 0

< Content-Type: text/html; charset=UTF-8

<

  • Connection #0 to host 172.19.0.10 left intact

Even if the section about URL scheme and supported methods states that PUT should work for creating objects, the host creation example uses POST. Would you try changing this?

Furthermore, your Accept header looks a bit off. Shouldn’t it be ,charset=utf-8 instead of ,charset:utf-8?

More information should hopefully be included in the logs. Thus, if trying to tweak these two does not help, please take a look in there.

Hello,
thanks for reply.

I tried as suggested but the result is the same.
It seems that conjuntion of /director/host path and PUT (or POST) isn’t correctly redirect (so the request is redirect to logn page):

root@rgserver:/docker-apps$ curl -u api:api -H’Accept:application/json,charset=utf-8’ -X POST ‘http://172.19.0.10:8080/director/host’ -d ‘{“object_name”: “prova_host”, “imports”: “host-template”, “display_name”: “prova_host”, “address”: “127.0.0.1”, “zone”: “satellite”}’ -v

  • About to connect() to 172.19.0.10 port 8080 (#0)
  • Trying 172.19.0.10…
  • Connected to 172.19.0.10 (172.19.0.10) port 8080 (#0)
  • Server auth using Basic with user ‘api’

POST /director/host HTTP/1.1
Authorization: Basic YXBpOmFwaQ==
User-Agent: curl/7.29.0
Host: 172.19.0.10:8080
Accept:application/json,charset=utf-8
Content-Length: 132
Content-Type: application/x-www-form-urlencoded

  • upload completely sent off: 132 out of 132 bytes
    < HTTP/1.1 302 Found
    < Date: Tue, 08 Oct 2024 07:49:07 GMT
    < Server: Apache/2.4.56 (Debian)
    > < Location: /authentication/login?redirect=director%2Fhost
    < Content-Length: 0
    < Content-Type: text/html; charset=UTF-8
    <
  • Connection #0 to host 172.19.0.10 left intact
    root@rgserver:/docker-apps$

Additional info:

icingaweb container log register these line:

GET:

172.19.0.1 - api [08/Oct/2024:10:58:15 +0000] “GET /director/host?name=icinga-satellite HTTP/1.1” 200 512 “-” “curl/7.29.0”

PUT:

172.19.0.1 - api [08/Oct/2024:10:58:48 +0000] “PUT /director/host HTTP/1.1” 302 208 “-” “curl/7.29.0”

I’d like to view apache2 log (on icingaweb container), but they are symbolic link to /dev/stdout and stderr

do you have rewritten the urls yourself or are you missing icingaweb2:

172.19.0.10:8080/icingaweb2/director/host?name=icinga-satellite

Icingaweb is a container, it esposes the port 8080 without “/icingaweb/”.
I also use 172.19.0.10:8080 to reach the web GUI.

you are missing

“object_type”: “object”

 '{"object_name": "prova_host", "object_type": "object", "imports": "host-template", "display_name": "prova_host", "address": "127.0.0.1", "zone": "satellite"}'

also for put and post you need to add the host to the url otherwise icinga director thinks you want to recreate a host

director/host?name=prova_host’

1 Like

you are missing

“object_type”: “object”

Your are right. I re-tried with in but result is the same.

director/host?name=prova_host’

I’m not sure about this, I use similar Icinga stack at work (not on container and to older release) and its works without. However I have tried with but result is the same.

Output:

#added “object_type”: “object”: and “?name=prova_host’”

curl -u api:api -H’Accept:application/json,charset:utf-8’ -X PUT ‘http://172.19.0.10:8080/director/host?name=prova_host’ -d ‘{“object_type”: “object”, “object_name”: “prova_host”, “imports”: “host-template”, “display_name”: “prova_host”, “address”: “127.0.0.1”, “zone”: “satellite”}’ -v

  • About to connect() to 172.19.0.10 port 8080 (#0)
  • Trying 172.19.0.10…
  • Connected to 172.19.0.10 (172.19.0.10) port 8080 (#0)
  • Server auth using Basic with user ‘api’

PUT /director/host?name=prova_host HTTP/1.1
Authorization: Basic YXBpOmFwaQ==
User-Agent: curl/7.29.0
Host: 172.19.0.10:8080
Accept:application/json,charset:utf-8
Content-Length: 157
Content-Type: application/x-www-form-urlencoded

  • upload completely sent off: 157 out of 157 bytes
    < HTTP/1.1 302 Found
    < Date: Tue, 08 Oct 2024 13:09:24 GMT
    < Server: Apache/2.4.56 (Debian)
    < Location: /authentication/login?redirect=director%2Fhost%3Fname%3Dprova_host
    < Content-Length: 0
    < Content-Type: text/html; charset=UTF-8
    <
  • Connection #0 to host 172.19.0.10 left intact

#added only “object_type”: “object”:

curl -u api:api -H’Accept:application/json,charset:utf-8’ -X PUT ‘http://172.19.0.10:8080/director/host’ -d ‘{“object_type”: “object”, “object_name”: “prova_host”, “imports”: “host-template”, “display_name”: “prova_host”, “address”: “127.0.0.1”, “zone”: “satellite”}’ -v

  • About to connect() to 172.19.0.10 port 8080 (#0)
  • Trying 172.19.0.10…
  • Connected to 172.19.0.10 (172.19.0.10) port 8080 (#0)
  • Server auth using Basic with user ‘api’

PUT /director/host HTTP/1.1
Authorization: Basic YXBpOmFwaQ==
User-Agent: curl/7.29.0
Host: 172.19.0.10:8080
Accept:application/json,charset:utf-8
Content-Length: 157
Content-Type: application/x-www-form-urlencoded

  • upload completely sent off: 157 out of 157 bytes
    < HTTP/1.1 302 Found
    < Date: Tue, 08 Oct 2024 13:09:37 GMT
    < Server: Apache/2.4.56 (Debian)
    < Location: /authentication/login?redirect=director%2Fhost
    < Content-Length: 0
    < Content-Type: text/html; charset=UTF-8
    <
  • Connection #0 to host 172.19.0.10 left intact

just too make sure you can login using api api in icingaweb2?

Yes,
I can login to Icingaweb GUI using api/api (user/password).

Api user have more permissions then required (to exclude permissions problem I given it full access)

now remove the charset thing

Test wthout “charset:utf-8”:

curl -u api:api -H’Accept:application’ -X PUT ‘http://172.19.0.10:8080/director/host’ -d ‘{“object_type”:“object”, “object_name”: “prova_host”, “imports”: “host-template”, “display_name”: “prova_host”, “address”: “127.0.0.1”}’ -v

  • About to connect() to 172.19.0.10 port 8080 (#0)
  • Trying 172.19.0.10…
  • Connected to 172.19.0.10 (172.19.0.10) port 8080 (#0)
  • Server auth using Basic with user ‘api’

PUT /director/host HTTP/1.1
Authorization: Basic YXBpOmFwaQ==
User-Agent: curl/7.29.0
Host: 172.19.0.10:8080
Accept:application
Content-Length: 135
Content-Type: application/x-www-form-urlencoded

  • upload completely sent off: 135 out of 135 bytes
    < HTTP/1.1 302 Found
    < Date: Tue, 08 Oct 2024 13:41:28 GMT
    < Server: Apache/2.4.56 (Debian)
    < Location: /authentication/login?redirect=director%2Fhost
    < Content-Length: 0
    < Content-Type: text/html; charset=UTF-8
    <
  • Connection #0 to host 172.19.0.10 left intact

you removed too much

‘Accept:application/json’

use that if the object exists:

curl -u 'api:api' -H 'Accept:application/json' -X POST 'http://172.19.0.10:8080/director/host?name=prova_host' -d '{"object_name": "prova_host", "object_type": "object", "imports": "host-template", "display_name": "prova_host", "address": "127.0.0.1"}' -v

or that if it does not exist

curl -u 'api:api' -H 'Accept:application/json' -X POST 'http://172.19.0.10:8080/director/host' -d '{"object_name": "prova_host", "object_type": "object", "imports": "host-template", "display_name": "prova_host", "address": "127.0.0.1"}' -v
1 Like

you removed too much

‘Accept:application/json’

goal!

Now it’s worked

curl -u api:api -H’Accept:application/json’ -X PUT ‘http://172.19.0.10:8080/director/host’ -d ‘{“object_type”: “object”, “object_name”: “prova_host2”, “imports”: “host-template”, “display_name”: “prova_host”, “address”: “127.0.0.1”, “zone”: “satellite”}’ -v

  • About to connect() to 172.19.0.10 port 8080 (#0)
  • Trying 172.19.0.10…
  • Connected to 172.19.0.10 (172.19.0.10) port 8080 (#0)
  • Server auth using Basic with user ‘api’

PUT /director/host HTTP/1.1
Authorization: Basic YXBpOmFwaQ==
User-Agent: curl/7.29.0
Host: 172.19.0.10:8080
Accept:application/json
Content-Length: 158
Content-Type: application/x-www-form-urlencoded

  • upload completely sent off: 158 out of 158 bytes
    < HTTP/1.1 201 Created
    < Date: Tue, 08 Oct 2024 13:53:26 GMT
    < Server: Apache/2.4.56 (Debian)
    < Content-Length: 201
    < Content-Type: application/json
    <
    {
    “address”: “127.0.0.1”,
    “display_name”: “prova_host”,
    “imports”: [
    “host-template”
    ],
    “object_name”: “prova_host2”,
    “object_type”: “object”,
    “zone”: “satellite”
    }
  • Connection #0 to host 172.19.0.10 left intact

Thank you very much!

At the end the solution was add in curl data “object_type”: “object”.

1 Like