twitteroauth icon indicating copy to clipboard operation
twitteroauth copied to clipboard

Media upload fails when user wants to decode json as array

Open le0m opened this issue 6 years ago • 5 comments

Hello, I found this bug because in my codebase I set the library to decode json responses as array with setDecodeJsonAsArray(true).

Doing so breaks the chunked media upload, because it consists of multiple separate calls and the method uploadMediaChunked() internally gives for granted that responses are stdClass objects.

see PR #755

le0m avatar Jan 30 '19 19:01 le0m

Interesting. Thanks for the PR. I'm wondering if maybe it would be better to allow http and makeRequests accept a boolean value to override $this->decodeJsonAsArray instead of casting.

abraham avatar Jan 30 '19 23:01 abraham

I agree with you that casting is not the best solution. Did the CI build fail because of this?

I used casting because uploadMediaChunked is the only method with this problem (other methods directly return Twitter responses), and it was the fastest solution for my usecase. Adding a parameter to http and makeRequest would work, but beware that both already accept a json parameter used for making requests.

le0m avatar Jan 31 '19 12:01 le0m

Currently the tests hit the live Twitter API so Travis will only run them when I create the PRs to avoid anyone from exfiltrating the credentials. #671 will fix this.

I'm not sure yet if overriding decodeJsonAsArray is the right pattern but I also don't like casting types.

abraham avatar Jan 31 '19 13:01 abraham

This is the makeRequests method https://github.com/abraham/twitteroauth/blob/6cc1b315ff8cdd7751a93ae873c7a29e2b2df37b/src/TwitterOAuth.php#L414-L415

it could be something like this:

$response = JsonDecoder::decode($result, false);
$this->response->setBody(JsonDecoder::decode($result, $this->decodeJsonAsArray));

and then in all API methods return $this->getLastBody(). The point is to separate the encoding used internally (in case the library itself needs to access a response parameter) from the encoding for the user.

le0m avatar Feb 01 '19 17:02 le0m

The problem is still actual :)

merkushin avatar Dec 15 '20 07:12 merkushin

TwitterOAuth is in maintenance mode and major improvements are no longer planned. https://github.com/abraham/twitteroauth/issues/1188

abraham avatar Jul 24 '23 03:07 abraham