Ad Widget

Collapse

trouble on API usage (user.login on Zabbix 2.2)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaopeng
    Junior Member
    • May 2015
    • 5

    #1

    trouble on API usage (user.login on Zabbix 2.2)

    This is my first time to try the API, and here is the command I tried:
    curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"user.login","params":{ "user":"Admin","password":"zabbix"},"auth":null,"i d":0}' http://1.1.1.1/zabbix/api_jsonrpc.php

    but this is what got:
    HTTP/1.0 412 Precondition Failed
    Date: Fri, 08 May 2015 08:36:08 GMT
    Server: Apache/2.2.22 (Linux/SUSE)
    X-Powered-By: PHP/5.3.17
    Content-Length: 0
    Connection: close
    Content-Type: text/html

    As I googled, everyone is saying the key is 'Content-Type:application/json', which I already paid enough attention to.

    I am stuck here, and am really puzzled on how to get more information for troubleshooting.


    My testing environment: Virutal Appliance v2.2
  • billjam
    Junior Member
    • Jan 2009
    • 27

    #2
    your command worked for me (after cahnging IP, username and password of course).
    You could try checking http error_log if it logs anything.

    Comment

    • brico
      Junior Member
      Zabbix Certified Specialist
      • Feb 2012
      • 10

      #3
      Working OK

      Which version of CURL are you using? I was able to get the command to succeed with Ubuntu 12.04:

      Code:
      curl 7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
      Code:
      curl -i -X POST -H 'Content-type:application/json' -d '{"jsonrpc":"2.0","method":"user.login","params":{ "user":"myUserName","password":"myPassword"},"auth":null,"id":0}' [URL]http://internal-zabbix.lan/api_jsonrpc.php[/URL]
      Returns:
      Code:
       
       {"jsonrpc":"2.0","result":"276a1fe0e6594edd2d345a487683bce8","id":0}
      Based on the response information you provided, it seems the header is not being passed correctly which could be an issue with the version of curl (guessing).

      Did you try putting the command in a file and executing it like a script? I noticed my Putty session was executing 'Content-tType' even though it was displaying 'Content-Type', resulting in "412 Precondition Failed". Having the command in a file will confirm the header is passed correctly.

      Comment

      • shaopeng
        Junior Member
        • May 2015
        • 5

        #4
        Originally posted by billjam
        your command worked for me (after cahnging IP, username and password of course).
        You could try checking http error_log if it logs anything.
        Thanks for your reply. I'd like to check the logs, but I am not familiar with Apache.
        Would you please which file should I go to for checking the http error_log?

        Comment

        • shaopeng
          Junior Member
          • May 2015
          • 5

          #5
          Originally posted by brico
          Which version of CURL are you using? I was able to get the command to succeed with Ubuntu 12.04:

          Code:
          curl 7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
          Code:
          curl -i -X POST -H 'Content-type:application/json' -d '{"jsonrpc":"2.0","method":"user.login","params":{ "user":"myUserName","password":"myPassword"},"auth":null,"id":0}' [URL]http://internal-zabbix.lan/api_jsonrpc.php[/URL]
          Returns:
          Code:
           
           {"jsonrpc":"2.0","result":"276a1fe0e6594edd2d345a487683bce8","id":0}
          Based on the response information you provided, it seems the header is not being passed correctly which could be an issue with the version of curl (guessing).

          Did you try putting the command in a file and executing it like a script? I noticed my Putty session was executing 'Content-tType' even though it was displaying 'Content-Type', resulting in "412 Precondition Failed". Having the command in a file will confirm the header is passed correctly.

          Thanks! I was using a curl on my Windows machines. I did a try on one of my CentOS servers. But this time I have a new problem. This is the response from the server:
          Code:
          HTTP/1.1 200 OK
          Date: Wed, 03 Jun 2015 06:43:25 GMT
          Server: Apache/2.2.22 (Linux/SUSE)
          X-Powered-By: PHP/5.3.17
          Content-Length: 2
          Content-Type: application/json
          I keep getting the same response even if I put a wrong password in the request.

          And here is my version info:
          Code:
          curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
          Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp 
          Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz
          I think this version should be ok, as it's the newest on CentOS, which is a little bit behind Ubuntu.

          Comment

          • billjam
            Junior Member
            • Jan 2009
            • 27

            #6
            I'm not sure if its just the line wrap in this forum or not , but make sure "id" doesn't have a space in it.

            "i d" gives me no response, but "id" with wrong password says:
            "data":"Login name or password is incorrect."

            The httpd log on centos is /var/log/httpd/ and either error_log or ssl_error_log or access...

            Comment

            • shaopeng
              Junior Member
              • May 2015
              • 5

              #7
              Originally posted by billjam
              I'm not sure if its just the line wrap in this forum or not , but make sure "id" doesn't have a space in it.

              "i d" gives me no response, but "id" with wrong password says:
              "data":"Login name or password is incorrect."

              The httpd log on centos is /var/log/httpd/ and either error_log or ssl_error_log or access...
              Thanks for your help. It works!
              I copy and paste from the forum, and that space between i and d caused the problem for my last try.
              Thanks again!

              Comment

              Working...