user1468768 user1468768 - 2 months ago 11
reST (reStructuredText) Question

Could not parse the 'params' argument as valid JSON (PHP, cURL, REST)

After executing the PHP Script, I am getting the following error – Any help is appreciated. Based on my understanding, I created an array based on the bugzilla documentation - Thanks in advance

https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#create-bug

{"code":32000,"error":true,"message":"Could not parse the 'params' argument as valid JSON. Error: malformed number (no digits after initial minus), at character offset 1

$url ="http://localhost:8080/bugzilla/rest/bug";

$data = array(
"product" => "TestProduct",
"component" => "TestComponent",
"version" => "unspecified",
"summary" => "'This is a test bug - please disregard",
"alias" => "SomeAlias",
"op_sys" => "All",
"priority" => "P1",
"rep_platform" => "All"
);

$str_data = json_encode($data);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
$username = 'ashish.sureka@in.abb.com';
$password = 'incrc';
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$result = curl_exec($ch);
curl_close($ch);

echo $result

Answer

You have this:

    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

which sends your array of data as a conventional key=value form submission, when you should have

    curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
                                         ^^^^^

which would send your JSON-encoded array instead.

Comments