[GH-ISSUE #33] JSON vs. x-www-form-urlencoded for PUT/POST #26

Closed
opened 2026-02-26 17:32:10 +03:00 by kerem · 8 comments
Owner

Originally created by @lukasbruha on GitHub (May 15, 2024).
Original GitHub issue: https://github.com/Corsinvest/cv4pve-api-php/issues/33

Hi,

thx for you lib. But I was wondering if json_encode($parameters) for PUT/POST methods is correct behavior since API doc, see https://pve.proxmox.com/wiki/Proxmox_VE_API#API_URL, specifies following:

Parameters can be passed using standard HTTP techniques:

  • via the URL
  • using x-www-form-urlencoded content-type for PUT and POST request.

As far as I can see, there is no JSON support for PUT/POST parameters. Currently, I am getting 501 Not Implemented when I want to call eg. POST /api2/json/nodes/NODE-01/qemu/103/status/stop via this library. But once I replace $data = json_encode($parameters) to eg. $data = http_build_query($parameters) in executeAction() and remove array_push for headers' Content-Type and Content-Length on lines that follows, server accepts it with 200 OK.

I am sure this library is using by several devs, so maybe it is about my Proxmox configuration. But since there is no official support of JSON mentioned in Proxmox documentation, I would kindly ask you for any response to this issue.

Thx.

Originally created by @lukasbruha on GitHub (May 15, 2024). Original GitHub issue: https://github.com/Corsinvest/cv4pve-api-php/issues/33 Hi, thx for you lib. But I was wondering if json_encode($parameters) for PUT/POST methods is correct behavior since API doc, see https://pve.proxmox.com/wiki/Proxmox_VE_API#API_URL, specifies following: Parameters can be passed using standard HTTP techniques: - via the URL - using x-www-form-urlencoded content-type for PUT and POST request. As far as I can see, there is no JSON support for PUT/POST parameters. Currently, I am getting 501 Not Implemented when I want to call eg. POST /api2/json/nodes/NODE-01/qemu/103/status/stop via this library. But once I replace $data = json_encode($parameters) to eg. $data = http_build_query($parameters) in [executeAction()](https://github.com/Corsinvest/cv4pve-api-php/blob/master/src/PveClientBase.php#L380) and remove array_push for headers' Content-Type and Content-Length on lines that follows, server accepts it with 200 OK. I am sure this library is using by several devs, so maybe it is about my Proxmox configuration. But since there is no official support of JSON mentioned in Proxmox documentation, I would kindly ask you for any response to this issue. Thx.
kerem closed this issue 2026-02-26 17:32:10 +03:00
Author
Owner

@franklupo commented on GitHub (May 15, 2024):

Hi,
what parameters do you pass?
can you attach an example?

best reagrds

<!-- gh-comment-id:2111709108 --> @franklupo commented on GitHub (May 15, 2024): Hi, what parameters do you pass? can you attach an example? best reagrds
Author
Owner

@lukasbruha commented on GitHub (May 15, 2024):

Hi, no parameters. The issue is in Content-Type and json_encode (although it is empty). Seems Proxmox API rejects it in general because of these both. FYI I am using Proxmox VE 8.1.4.

In code, see https://github.com/Corsinvest/cv4pve-api-php/blob/master/src/PveClientBase.php#L380, there is no decision to not pass empty data if not defined. It passes and sets Content-Type everytime. So I cannot avoid that.

<!-- gh-comment-id:2111736771 --> @lukasbruha commented on GitHub (May 15, 2024): Hi, no parameters. The issue is in Content-Type and json_encode (although it is empty). Seems Proxmox API rejects it in general because of these both. FYI I am using Proxmox VE 8.1.4. In code, see https://github.com/Corsinvest/cv4pve-api-php/blob/master/src/PveClientBase.php#L380, there is no decision to not pass empty data if not defined. It passes and sets Content-Type everytime. So I cannot avoid that.
Author
Owner

@franklupo commented on GitHub (May 15, 2024):

is the solution not to pass parameters if they aren't there?

<!-- gh-comment-id:2111773714 --> @franklupo commented on GitHub (May 15, 2024): is the solution not to pass parameters if they aren't there?
Author
Owner

@lukasbruha commented on GitHub (May 15, 2024):

I am not sure since JSON as input is not officially supported according to documentation. If method was protected instead of private, I would overwrite it myself to avoid JSON for PUT/POST completely and pass query instead although I think JSON is more pretty and would be suitable when Proxmox responses are in JSON as well.

Question: when you developed this lib, JSON was working with Proxmox POST call as mentioned above? I was wondering why I am the only one having these issues. Can you test it yourself in your dev env with Proxmox?

<!-- gh-comment-id:2111878920 --> @lukasbruha commented on GitHub (May 15, 2024): I am not sure since JSON as input is not officially supported according to documentation. If method was protected instead of private, I would overwrite it myself to avoid JSON for PUT/POST completely and pass query instead although I think JSON is more pretty and would be suitable when Proxmox responses are in JSON as well. Question: when you developed this lib, JSON was working with Proxmox POST call as mentioned above? I was wondering why I am the only one having these issues. Can you test it yourself in your dev env with Proxmox?
Author
Owner

@franklupo commented on GitHub (May 15, 2024):

check the problem and I'll update you

<!-- gh-comment-id:2112160356 --> @franklupo commented on GitHub (May 15, 2024): check the problem and I'll update you
Author
Owner

@lukasbruha commented on GitHub (May 16, 2024):

Hi, I have (possibly) find a solution (you have mentioned it above): when there are no parameters, you should avoid empty JSON and CURLOPT_POSTFIELDS should be set to empty string (as defined on https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html). When there are any parameter, JSON can be sent although Proxmox API doc does not mention it explicitly on https://pve.proxmox.com/wiki/Proxmox_VE_API#API_URL

So the fix in https://github.com/Corsinvest/cv4pve-api-php/blob/master/src/PveClientBase.php#L378 should be:

$data = ""; // init 
...
case "POST":
	curl_setopt($prox_ch, CURLOPT_POST, 1);
	
	// data from params only if there are any
	if (count($params)) { 
		$data = json_encode($params);
		array_push($headers, 'Content-Type: application/json');
		array_push($headers, 'Content-Length: ' . strlen($data));
	}

	curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $data);
	$methodType = "CREATE";
	break;
...

Analogically for PUT switch-case branch in https://github.com/Corsinvest/cv4pve-api-php/blob/master/src/PveClientBase.php#L369.

Does it make sense to you?

<!-- gh-comment-id:2114723667 --> @lukasbruha commented on GitHub (May 16, 2024): Hi, I have (possibly) find a solution (you have mentioned it above): when there are no parameters, you should avoid empty JSON and CURLOPT_POSTFIELDS should be set to empty string (as defined on https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html). When there are any parameter, JSON can be sent although Proxmox API doc does not mention it explicitly on https://pve.proxmox.com/wiki/Proxmox_VE_API#API_URL So the fix in https://github.com/Corsinvest/cv4pve-api-php/blob/master/src/PveClientBase.php#L378 should be: ``` $data = ""; // init ... case "POST": curl_setopt($prox_ch, CURLOPT_POST, 1); // data from params only if there are any if (count($params)) { $data = json_encode($params); array_push($headers, 'Content-Type: application/json'); array_push($headers, 'Content-Length: ' . strlen($data)); } curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $data); $methodType = "CREATE"; break; ... ``` Analogically for PUT switch-case branch in https://github.com/Corsinvest/cv4pve-api-php/blob/master/src/PveClientBase.php#L369. Does it make sense to you?
Author
Owner

@franklupo commented on GitHub (May 16, 2024):

Yes, if you want create a pull request

<!-- gh-comment-id:2114906027 --> @franklupo commented on GitHub (May 16, 2024): Yes, if you want create a pull request
Author
Owner

@lukasbruha commented on GitHub (May 17, 2024):

Done, see https://github.com/Corsinvest/cv4pve-api-php/pull/34. Please merge it once you have time.

<!-- gh-comment-id:2117415677 --> @lukasbruha commented on GitHub (May 17, 2024): Done, see https://github.com/Corsinvest/cv4pve-api-php/pull/34. Please merge it once you have time.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/cv4pve-api-php#26
No description provided.