Skip to content

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

Closed
@jeroennoten

Description

@jeroennoten

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions