Ad Widget

Collapse

API Help: JSON-rpc version not specified

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parasin
    Member
    Zabbix Certified Specialist
    • Dec 2014
    • 53

    #1

    API Help: JSON-rpc version not specified

    Hey all,

    working on creating a script that can automatically synchronize my Zabbix servers by exporting the template data on one server, then importing that data to another Zabbix server.

    I'm getting an error that i can't figure out when I try to run some Javascript, no matter what method I try:
    JSON-rpc version is not specified.
    Here is my Javascript:
    Code:
    function auth() {
        var auth;
        var url = 'http://<ip>/zabbix/api_jsonrpc.php';
        
        var req = {
            "jsonrpc":"2.0",
            "method":"user.login",
            "params": {
                "user":"<username>",
                "passowrd":"<password>"
            },
            "auth":null,
            "id":1
        };
        console.log(req);
        $.ajax({
    		type: 'POST',
    		data: req,
    		contentType: 'application/json-rpc',
    		url: url,
    		success: function(data) {
                console.log(data);
                if(!data.error) {
                    $(" #results ").text(data);
                }
                else {
                    $(" #results ").text(data.error.data);
                }
    	   },
            error: function(data) {
                $(" #results ").text(data.error.data);
            }
        });
    }
    
    auth();

    The weird part is, when I run this curl command:
    Code:
    curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"user.login","params":{"user":"<username>","password":"<password>"},"id":1,"auth":null}' http://<ip>/zabbix/api_jsonrpc.php
    I get the correct results back:
    HTTP/1.1 200 OK
    Date: Tue, 29 Mar 2016 20:57:06 GMT
    Server: Apache/2.2.15 (CentOS)
    X-Powered-By: PHP/5.3.3
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Headers: Content-Type
    Access-Control-Allow-Methods: POST
    Access-Control-Max-Age: 1000
    Content-Length: 68
    Content-Type: application/json
    Proxy-Connection: Keep-Alive
    Connection: Keep-Alive

    {"jsonrpc":"2.0","result":"afcfbf67da00669c91b355e b84834372","id":1}

    Any idea as to how I can adjust my Javascript to fix this error??
  • Parasin
    Member
    Zabbix Certified Specialist
    • Dec 2014
    • 53

    #2
    Solved!

    Ok, so if anyone else encounters this issue, here is what I did to resolve it.

    It appears that the Zabbix API actually parses a JSON string it does not directly take JSON-formatted data. So, if you are using Javascript to handle the connection to the Zabbix API, you have to send it as a string, formatted as JSON.

    Example:

    Code:
    var req = {
            "jsonrpc":"2.0",
            "method":"apiinfo.version",
            "params": {
                "user":"<username>",
                "passowrd":"<password>"
            },
            "auth":null,
            "id":1
        };
        
        // Here is the AJAX call to the Zabbix API
        $.ajax({
    		type: 'POST',
    		[B]data: JSON.stringify(req)[/B],  //THIS LINE IS WHERE THE MAGIC HAPPENS
    		contentType: 'application/json',
    		url: url,
    	        success: function(data) {
                        console.log(data);
                        if(!data.error) {
                             $(" #results ").text(JSON.stringify(data));
                        }
                        else {
                            $(" #results ").text(data.error.data);
                        }
    	        },
                    error: function(data) {
                            $(" #results ").text(data.error.data);
                   }
        });

    That code returns the following results:

    Code:
    {"jsonrpc":"2.0","result":"2.4.4","id":1}
    So in summary, you need to use the
    Code:
    JSON.stringify()
    method on your data before attempting to pass it to the Zabbix API.

    Comment

    Working...