Skip to content

Commit c61e0e4

Browse files
authored
Merge pull request #34 from lukasbruha/master
empty JSON in PUT/POST calls fix
2 parents 6dee43a + 7af52b4 commit c61e0e4

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/PveClientBase.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ private function executeAction($resource, $method, $parameters = [])
357357

358358
$headers = [];
359359
$methodType = "";
360+
$data = ""; // default POSTFIELDS value as defined in https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html
360361
$prox_ch = curl_init();
361362
switch ($method) {
362363
case "GET":
@@ -368,18 +369,28 @@ private function executeAction($resource, $method, $parameters = [])
368369

369370
case "PUT":
370371
curl_setopt($prox_ch, CURLOPT_CUSTOMREQUEST, "PUT");
371-
$data = json_encode($params);
372-
array_push($headers, 'Content-Type: application/json');
373-
array_push($headers, 'Content-Length: ' . strlen($data));
372+
373+
// data from params only if there are any
374+
if (count($params)) {
375+
$data = json_encode($params);
376+
array_push($headers, 'Content-Type: application/json');
377+
array_push($headers, 'Content-Length: ' . strlen($data));
378+
}
379+
374380
curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $data);
375381
$methodType = "SET";
376382
break;
377383

378384
case "POST":
379385
curl_setopt($prox_ch, CURLOPT_POST, true);
380-
$data = json_encode($params);
381-
array_push($headers, 'Content-Type: application/json');
382-
array_push($headers, 'Content-Length: ' . strlen($data));
386+
387+
// data from params only if there are any
388+
if (count($params)) {
389+
$data = json_encode($params);
390+
array_push($headers, 'Content-Type: application/json');
391+
array_push($headers, 'Content-Length: ' . strlen($data));
392+
}
393+
383394
curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $data);
384395
$methodType = "CREATE";
385396
break;

0 commit comments

Comments
 (0)