Skip to content

Http redirects are not followed when using caching http client #38207

Closed
@dmolineus

Description

@dmolineus

Symfony version(s) affected: ^4.3.0

Description

I use the http client for a GET request. By default an instance of Symfony\Component\HttpClient\HttpClient follows redirects (by default up to 20). When using the Symfony\Component\HttpClient\CachingHttpClient every redirect leads to an exception.

The issue is caused by the Symfony\Component\HttpKernel\HttpClientKernel which overrides the max_redirects option:

$response = $this->client->request($request->getMethod(), $request->getUri(), [
'headers' => $headers,
'body' => $body,
'max_redirects' => 0,
] + $request->attributes->get('http_client_options', []));

How to reproduce

<?php

$client = Symfony\Component\HttpClient\HttpClient::create();
$cachingClient = new Symfony\Component\HttpClient\CachingHttpClient(
    $client,
    new Symfony\Component\HttpKernel\HttpCache\Store('/path/to/the/cache') 
);

// Work as expectede
$client->request(
    'GET', 
    // Redirect to https://api.github.com/repositories/109533757/git/matching-refs/tags/ happens here
    'https://api.github.com/repos/humbug/box/git/matching-refs/tags/', 
    ['headers' => ['Authorization' => 'token XXX']
);

// Throws Symfony\Component\HttpClient\Exception\RedirectionException
$cachingClient->request(
    'GET', 
    // Redirect to https://api.github.com/repositories/109533757/git/matching-refs/tags/ happens here
    'https://api.github.com/repos/humbug/box/git/matching-refs/tags/', 
    ['headers' => ['Authorization' => 'token XXX']
);

Additional context

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