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

cURL.php prepareRequest - wrong behavior #61

Closed
akoevroman opened this issue Dec 11, 2018 · 5 comments
Closed

cURL.php prepareRequest - wrong behavior #61

akoevroman opened this issue Dec 11, 2018 · 5 comments

Comments

@akoevroman
Copy link

akoevroman commented Dec 11, 2018

In several cases: $request->hasData() return true for empty(default) array of data.
This happens if $request->encodeData() return string '[]'
In this case, empty CURLOPT_POSTFIELDS are sent.

Also Request.php -> encodeData function has $encoding variable which is not defined.

@djcvijic
Copy link

djcvijic commented Dec 11, 2018

I recently implemented this behaviour so maybe I can help.

This is important for JSON communication, because empty arrays and empty objects are encoded to '[]' and '{}' respectively, and this is NON-empty data, hasData MUST be true, and CURLOPT_POSTFIELDS MUST be sent.

Please state your exact usage, the expected result, and the actual result, so that we may find a universal solution.

@anlutro
Copy link
Owner

anlutro commented Dec 13, 2018

What @djcvijic wrote is indeed true!

@akoevroman what version of the library are you using? This should be fixed in 1.4.5 or higher.

I pushed a commit for the $encoding variable issue you mentioned, though that default case should never actually trigger so I don't think it's a fatal bug.

@akoevroman
Copy link
Author

akoevroman commented Dec 14, 2018

Version 1.4.6

Example:

$request = cURL::newJsonRequest('get', $this->requestUrl);
$response = $request->send();

so then we go to lib

public function newJsonRequest($method, $url, $data = array())
	{
		return $this->newRequest($method, $url, $data, Request::ENCODING_JSON);
	}

array() by default

then
public function newRequest($method, $url, $data = array(), $encoding = Request::ENCODING_QUERY)
also array

Finaly:

array->
$request->setData($data);
->
$this->data = $data;
->
$request->hasData()
-> '[]' returned

maybe... i did something wrong?

Of course, "'[]' returned" is not a critical error but some API denied empty array in POST fields and return a response that appropriate data are expected.

P.S. Maybe if I will use 3rd parameter and set it to '' it will work, but this is not obvious logic. And i did not try it.

@anlutro
Copy link
Owner

anlutro commented Dec 14, 2018

That's an annoying bug. See the commit message of a2809a2 for more information. I've tagged 1.4.7 with the fix.

@djcvijic
Copy link

Yes, the recent fix I made was to cover POST-like request methods, but then GET was broken in the process. 1.4.7 looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants