Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] Response::toArray() throws TransportException instead of JsonException when response body is empty #37064

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

Closed
jeroennoten opened this issue Jun 2, 2020 · 2 comments

Comments

@jeroennoten
Copy link
Contributor

Symfony version(s) affected: 4.3.0 - 5.1.0

Description
When calling toArray() on a response with an empty body, a TransportException is thrown. A TransportException should be thrown on network errors. However, responses with empty body might be completely valid. In that case, toArray() should throw JsonException (or other exception that implements DecodingExceptionInterface) should be thrown, because the reponse body does not contain valid JSON.

How to reproduce

$httpClient = HttpClient::create();
$response = $httpClient->request('GET', 'https://httpbin.org/status/201');
$response->toArray(); // throws TransportException with message 'Response body is empty.'

Possible Solution
Throw a JsonException instead of a TransportException when response body is empty.

@jeroennoten jeroennoten added the Bug label Jun 2, 2020
@jeroennoten jeroennoten changed the title [Http-Client] Response::toArray() throws TransportException instead of JsonException when response body is empty [HttpClient] Response::toArray() throws TransportException instead of JsonException when response body is empty Jun 2, 2020
@Gemorroj
Copy link
Contributor

Gemorroj commented Jun 2, 2020

Maybe better make toArray nullable for empty body?

@fabpot fabpot closed this as completed Jun 3, 2020
fabpot added a commit that referenced this issue Jun 3, 2020
…tion on empty response in Response::toArray() (jeroennoten)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpClient] Throw JsonException instead of TransportException on empty response in Response::toArray()

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37064
| License       | MIT

Commits
-------

69547d9 [HttpClient] Throw JsonException instead of TransportException on empty response in Response::toArray()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants