-1

I'm sending a request to a JSON API system (http://help.solarwinds.com/backup/documentation/Content/service-management/json-api/login.htm) using PHP:

$base = 'https://cloudbackup.management/jsonapi';

$vars = array(
    "jsonrpc" => "2.0",
    "method" => "Login",
    "params" => array(
        "partner" => "partner",
        "username" => "username",
        "password" => "pass",
    ),
    "id" => "1",
);

$ch = curl_init( $base );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$output = json_decode($response, true);

But its returning this array in $output

Array
(
    [error] => Array
        (
            [code] => -32700
            [data] => 119
            [message] => Parse error: Failed to parse request body: * Line 1, Column 1
  '--------------------------' is not a number.

        )

    [id] => jsonrpc
    [jsonrpc] => 2.0
)

I cannot work out why it's returning an error, because I'm sending the correct parameters that it says in the docs.

Can someone point me in the right direction or if I have missed something?

charlie
  • 415
  • 4
  • 35
  • 83

1 Answers1

1

Set the content-type to application/json as curl is likely defaulting to sending it as x-www-form-urlencoded

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

also JSON-encode your array:

$jsonDataEncoded = json_encode($vars);

Full refactored sample:

$base = 'https://cloudbackup.management/jsonapi';

$vars = array(
    "jsonrpc" => "2.0",
    "method" => "Login",
    "params" => array(
        "partner" => "partner",
        "username" => "username",
        "password" => "pass",
    ),
    "id" => "1",
);

$jsonDataEncoded = json_encode($jsonData);
$ch = curl_init( $base );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
$response = curl_exec($ch);
curl_close($ch);

$output = json_decode($response, true);
Sean Murphy
  • 516
  • 4
  • 7