Ad Widget

Collapse

API. Method 'host.create'. Error 'Invalid Request.'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stincv
    Junior Member
    • Nov 2015
    • 1

    #1

    API. Method 'host.create'. Error 'Invalid Request.'

    Hi.

    The script looks like this:
    PHP Code:
    $url "http://***/zabbix/api_jsonrpc.php";

    function 
    send_request($url,$json) {
        
    $ch curl_init();
        
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
        
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
    curl_setopt($chCURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        
    curl_setopt($chCURLOPT_URL,$url);
        
    curl_setopt($chCURLOPT_POST1);
        
    curl_setopt($chCURLOPT_POSTFIELDS,$json);
        
    curl_setopt($chCURLOPT_USERPWD"***:***");
        
    $result=curl_exec($ch);
        
    curl_close($ch);
        return 
    json_decode($result);
    }

    $json '{
       "jsonrpc": "2.0",
       "method": "user.login",
       "params": {
          "user": "admin",
          "password": "***"
       },
       "id":1
    }'
    ;

    $res send_request($url,$json);
    $auth $res->result;
    var_dump($auth);

    $json '{
        "jsonrpc": "2.0",
        "method": "host.create",
        "params": {
            "host": "domain.ru",
            "interfaces": [
                {
                    "type": 1,
                    "main": 1,
                    "useip": 1,
                    "ip": "192.168.0.1",
                    "dns": "domain.ru",
                    "port": "10050"
                }
            ],
            "groups": [
                {
                    "groupid": "9"
                }
            ],
            "templates": [
                {
                    "templateid": "10130"
                }
            ],
        },
        "auth": "'
    .$auth.'",
        "id":1
    }'
    ;

    $res send_request($url,$json);
    var_dump($res); 
    The output is:
    PHP Code:
    string(32"c96ceaf1051c3410a43f73541f8c1c05"                                                                                                                                           
    object(stdClass)#2 (3) {                                                                                                                                                                
      
    ["jsonrpc"]=>                                                                                                                                                                         
      
    string(3"2.0"                                                                                                                                                                       
      
    ["error"]=>                                                                                                                                                                           
      
    object(stdClass)#3 (3) {                                                                                                                                                              
        
    ["code"]=>                                                                                                                                                                                              
        
    int(-32600)                                                                                                                                                                                             
        [
    "message"]=>                                                                                                                                                                                           
        
    string(16"Invalid Request."                                                                                                                                                                                                     
        
    ["data"]=>                                                                                                                                                                                                                        
        
    string(34"JSON-rpc version is not specified."                                                                                                                                                                                   
      
    }                                                                                                                                                                                                                                   
      [
    "id"]=>                                                                                                                                                                                                                                                            
      
    NULL                                                                                                                                                                                                                                                                

    As can be seen the first authorization request passes but the second request "----" does not pass, although the request was for the documents and errors there may be no.
    Help, why the error?

    ==============================
    I found a solution to the problem itself
    after the last element of the array must not be a comma
    Last edited by stincv; 11-11-2015, 14:37. Reason: I found a solution to the problem itself
Working...