twitteroauth
twitteroauth copied to clipboard
Media upload fails when user wants to decode json as array
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
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.
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.
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.
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.
The problem is still actual :)
TwitterOAuth is in maintenance mode and major improvements are no longer planned. https://github.com/abraham/twitteroauth/issues/1188