Philipp Gebert
Philipp Gebert

Reputation: 131

Symfony: Define scoped HttpClient class

In my Symfony 5.1 app, I am using several scoped http clients, defined in my framework.yaml:

framework:
    http_client:
        scoped_clients:
            my_client:
                  ....
            example_client:
                base_uri: 'https://example.com/...'
                headers:
                    Accept: 'application/json'
                    Connection: 'keep-alive'
            ....

How can I configure symfony to use CurlHttpClient for "my_client" and NativeHttpClient for "example_client"?

Upvotes: 3

Views: 3529

Answers (1)

es cologne
es cologne

Reputation: 3614

It is enough to add this Scoped Client in your service class:

public function __construct(HttpClientInterface $exampleClient)
{
    $this->httpClient = $exampleClient;
}

As you can see $exampleClient corresponds to example_client .

See https://symfony.com/doc/current/http_client.html#scoping-client (yes, this documentation is inaccurate)

Upvotes: 0

Related Questions