Skip to content

[HttpClient] AsyncResponse: Underlying response still checks status code on destruct even after getContent(false) #37663

@bohanyang

Description

@bohanyang

Symfony version(s) affected: 5.2

Description
AsyncResponse: Underlying response still checks status code on destruct even after AsyncResponse::getContent(false)

How to reproduce

<?php

declare(strict_types=1);

use Symfony\Component\HttpClient\AsyncDecoratorTrait;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Response\AsyncContext;
use Symfony\Component\HttpClient\Response\AsyncResponse;
use Symfony\Contracts\HttpClient\ChunkInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

require __DIR__ . '/vendor/autoload.php';

$client = new class(HttpClient::create()) implements HttpClientInterface {
    use AsyncDecoratorTrait;

    public function request(string $method, string $url, array $options = []) : ResponseInterface
    {
        return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) {
            yield $chunk;
        });
    }
};

$response = $client->request('GET', 'https://httpbin.org/status/404');
echo $response->getContent(false);

Is this expected behavior??

PHP Fatal error:  Uncaught Symfony\Component\HttpClient\Exception\ClientException: HTTP/2 404  returned for "https://httpbin.org/status/404". in C:\Users\User\Desktop\Workspace\RetryHttpClient\vendor\symfony\http-client\Response\CommonResponseTrait.php:176
Stack trace:
#0 C:\Users\User\Desktop\Workspace\RetryHttpClient\vendor\symfony\http-client\Response\TransportResponseTrait.php(132): Symfony\Component\HttpClient\Response\CurlResponse->checkStatusCode()
#1 C:\Users\User\Desktop\Workspace\RetryHttpClient\vendor\symfony\http-client\Response\CurlResponse.php(218): Symfony\Component\HttpClient\Response\CurlResponse->doDestruct()
#2 [internal function]: Symfony\Component\HttpClient\Response\CurlResponse->__destruct()
#3 {main}
  thrown in C:\Users\User\Desktop\Workspace\RetryHttpClient\vendor\symfony\http-client\Response\CommonResponseTrait.php on line 176

Possible Solution
bypass it with $this->response->getStatusCode() on AsyncResponse::__destruct ?

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