Ad Widget

Collapse

API connection is not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • b3owu1f
    Junior Member
    • Mar 2022
    • 7

    #1

    API connection is not working

    Greetings everyone,

    I'm trying to connect to Zabbix through API but it seems I can't get anything done. All forms I tried result in errors, even from inside the server. I tried many ways to retrieve apiiversion and to use user.login.
    My frontend is at http://192.168.0.100/zabbix
    I did not install the frontend. It was given to me to work on. Can it be any issue with the installation? There's anything I need to configure?

    Zabbix version is 5.0.16 and the api files are found on following places:

    # find . -name api_jsonrpc.php
    ./opt/zabbix-backup/zabbix/api_jsonrpc.php
    ./opt/comp/zabbix-5.0.16/ui/api_jsonrpc.php
    ./usr/share/zabbix/api_jsonrpc.php

    ----

    I tried using curl from inside the server, to localhost and real IP, didn't work:

    curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc":"2.0","method":"apiinfo.version","id ": 1,"auth":null,"params":{}}' 'http://127.0.0.1/zabbix/api_jsonrpc.php'
    curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc":"2.0","method":"apiinfo.version","id ": 1,"auth":null,"params":{}}' 'http://192.168.0.100/zabbix/api_jsonrpc.php'

    And results are:

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>302 Found</title>
    </head><body>
    <h1>Found</h1>
    <p>The document has moved <a href="https://127.0.0.1/api_jsonrpc.php">here</a>.</p>
    </body></html>

    ----

    I tried through Postman app too:


    POST http://192.168.0.100/zabbix/api_jsonrpc.php

    Headers:
    Content-Type: application/json

    Body: raw

    {
    "jsonrpc":"2.0",
    "method":"apiinfo.version",
    "params":{},
    "id":1
    }

    And results are:

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>

    <head>
    <title>400 Bad Request</title>
    </head>

    <body>
    <h1>Bad Request</h1>
    <p>Your browser sent a request that this server could not understand.<br/>
    </p>
    </body>

    </html>
    Last edited by b3owu1f; 20-07-2022, 00:21.
  • Markku
    Senior Member
    Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
    • Sep 2018
    • 1781

    #2
    302 Found
    ...
    The document has moved <a href="https://127.0.0.1/api_jsonrpc.php">here</a>
    So, is it http or https?

    Your Postman test is missing the auth field, if that matters.

    To be sure, the documentation is: https://www.zabbix.com/documentation/5.0/en/manual/api

    Markku

    Comment


    • b3owu1f
      b3owu1f commented
      Editing a comment
      When I access the frontend it shows https. I tried the CURL either way with http and https. https returns nothing.
      The spaces are being added by the forum. there's no spaces in the CURL.

      [root@zbx ~]# curl -s -X POST -H 'Content-Type:application/json' -d '{"params":{},"jsonrpc":"2.0","method":"apiinfo.ve rsion","id":1,"auth":null}' 'http://127.0.0.1/zabbix/api_jsonrpc.php'
      <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
      <html><head>
      <title>302 Found</title>
      </head><body>
      <h1>Found</h1>
      <p>The document has moved <a href="https://127.0.0.1/zabbix/api_jsonrpc.php">here</a>.</p>
      </body></html>
      [root@zbx ~]# curl -s -X POST -H 'Content-Type:application/json' -d '{"params":{},"jsonrpc":"2.0","method":"apiinfo.ve rsion","id":1,"auth":null}' 'https://127.0.0.1/zabbix/api_jsonrpc.php'
      [root@zbx ~]#

    • Markku
      Markku commented
      Editing a comment
      -s option "hides" the certificate error from curl.
  • vladimir_lv
    Senior Member
    • May 2022
    • 240

    #3
    Click image for larger version

Name:	space.png
Views:	8949
Size:	21.2 KB
ID:	448227
    Just remove space after id and before 1

    Comment


    • Markku
      Markku commented
      Editing a comment
      This forum software is such a piece of fine art that it sometimes adds spaces of its own, you never know... worth pointing out anyway.

      Markku

    • b3owu1f
      b3owu1f commented
      Editing a comment
      The spaces are being added by the forum. there's no spaces in the CURL.
  • Markku
    Senior Member
    Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
    • Sep 2018
    • 1781

    #4
    Just testing: with the incorrect "id " parameter there is a specific message about the error:

    $ curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc":"2.0","method":"apiinfo.version","id ": 1,"auth":null,"params":{}}' http://192.168.7.15/zabbix/api_jsonrpc.php
    {"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request.","data":"Invalid parameter "/": unexpected parameter "id "."},"id":null}


    Without the space:

    $ curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc":"2.0","method":"apiinfo.version", "id": 1,"auth":null,"params":{}}' http://192.168.7.15/zabbix/api_jsonrpc.php
    {"jsonrpc":"2.0","result":"6.2.0","id":1}


    (ha, had to add an extra space to separate the parameters because the forum insisted on adding incorrect space otherwise)

    Markku

    Comment

    • Markku
      Senior Member
      Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
      • Sep 2018
      • 1781

      #5
      Originally posted by b3owu1f
      I tried through Postman app too:
      I'm (using 6.2.0) not able to get any 400 error with the inputs you showed us. Are you sure that your Zabbix frontend is in that address and not actually somewhere else? (Or, are you sure you used Postman properly)

      Markku
      Last edited by Markku; 20-07-2022, 08:57.

      Comment


      • b3owu1f
        b3owu1f commented
        Editing a comment
        Yes I'm sure it's the frontend, I access it normally. Not so sure I'm using the POSTMAN correctly, I just followed a tutorial since the CURL wasn't working. I did try with either http and https and same result.
    • b3owu1f
      Junior Member
      • Mar 2022
      • 7

      #6
      There's no space in the CURL. Idk why the forum added it when pasting here.
      I keep trying every tutorial I find and I can't get it to work. I really need it to. So any help is very much appreciated.
      Last edited by b3owu1f; 20-07-2022, 13:57.

      Comment

      • b3owu1f
        Junior Member
        • Mar 2022
        • 7

        #7
        Originally posted by Markku

        So, is it http or https?
        After you mentioned this, I remembered the server has no valid certificate for https as it says it's not safe connection. So I started looking around about Zabbix API on HTTPS and found this https://www.zabbix.com/forum/zabbix-...-api-via-https
        All the issue seems about not having a valid certificate on https. So I went to apache httpd.conf and removed the forcing http to https, and now I get data normally from the CURL on http.

        #RewriteEngine On
        #RewriteCond %{HTTPS} off
        #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

        Thanks for the insight. I hope this can help others who stumble upon this. I think this matter is finished.

        Comment

        • Markku
          Senior Member
          Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
          • Sep 2018
          • 1781

          #8
          Yes so using (properly configured) self-signed certificate for encryption is not a problem with Zabbix API. Just make sure that the client can handle the situation or trust the self-signed certificate.

          Markku

          Comment

          Working...