Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CURL Json POST #3489 #8373

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/internal/Magento/Framework/HTTP/Client/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,18 @@ public function getStatus()
* @param string $method
* @param string $uri
* @param array $params
* @param bool $json
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function makeRequest($method, $uri, $params = [])
protected function makeRequest($method, $uri, $params = [], $json = false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution to Magento 2 project.

Adding this logic to protected method requires extension developers inherit this class. This is what we are trying to avoid in Magento 2.

Could you please provide more details about your use case? Maybe we will suggest a better way how we can achieve your goal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@okorshenko how should we do get/post requests?

In our case we need to send post with json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got your point, we need to modify post, get method too. Not just this protected method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fixes. Unfortunately, by this fix you modified public interface of the class and added knowledge about JSON for Curl client. On the other hand, how to post XML request for instance? We don't want to add $isXML flag to the method again. So we need to have something more general - string format.

Let me suggest another approach:

 * @param array|string $params
protected function makeReques (...) {
 
   ...
   $this->curlOption(CURLOPT_POSTFIELDS, is_array($params) ? http_build_query($params) : $params);
   ...
}

We are extending our contract by adding new supported type string wihtout changes in public method parameters.

Please, do not forget to align makeRequest and post method signatures.

Thank you for reporting and fixing this issue. Good catch! +100500 to your karma! ;)

{
$this->_ch = curl_init();
$this->curlOption(CURLOPT_URL, $uri);
if ($method == 'POST') {
$this->curlOption(CURLOPT_POST, 1);
$this->curlOption(CURLOPT_POSTFIELDS, http_build_query($params));
$this->curlOption(CURLOPT_POSTFIELDS, $json ? json_encode($params) : http_build_query($params));
} elseif ($method == "GET") {
$this->curlOption(CURLOPT_HTTPGET, 1);
} else {
Expand Down