Skip to content

HttpClientConfig

Mutable, fluent configuration for a named HTTP client, populated inside a HttpClientFactory::configure() callback (the dotnet AddHttpClient("name", c => ...) analogue).

Turned into an immutable HttpClient once, then memoized by the factory.

final class HttpClientConfig

SourceHttp/Client/HttpClientConfig.php
MethodDescription
baseUri(string $baseUri): HttpClientConfigBase URI prepended to relative request paths (e.g.
getBaseUri(): stringReturns the configured base URI with any trailing slash removed, or an empty string when none was set.
getHeaders(): array<string, string>
getRetries(): intReturns how many extra attempts a transient failure earns; zero, the default, means no retrying.
getRetryBaseDelayMs(): intReturns the first backoff delay in milliseconds, from which later retries grow exponentially; 100 unless HttpClientConfig::retry() changed it.
getTransport(): ClientInterfaceReturns the PSR-18 transport this client will use.
header(string $name, string $value): HttpClientConfigA default header sent with every request unless the request already sets it.
headers(array<string, string> $headers): HttpClientConfig
retry(int $attempts, int $baseDelayMs = 100): HttpClientConfigRetry transient failures (network errors, HTTP 429, HTTP 5xx) up to $attempts extra times, with exponential backoff from $baseDelayMs.
transport(ClientInterface $transport): HttpClientConfigOverride the underlying PSR-18 transport for this client (default: TransportFactory::default()).

public function baseUri(string $baseUri): HttpClientConfig

Base URI prepended to relative request paths (e.g.

https://api.example.com”).

ParameterTypeDescription
$baseUristring

Returns HttpClientConfig

public function getBaseUri(): string

Returns the configured base URI with any trailing slash removed, or an empty string when none was set.

Returns string

public function getHeaders(): array<string, string>

Returns array``<``string``, ``string``>

public function getRetries(): int

Returns how many extra attempts a transient failure earns; zero, the default, means no retrying.

Returns int

public function getRetryBaseDelayMs(): int

Returns the first backoff delay in milliseconds, from which later retries grow exponentially; 100 unless HttpClientConfig::retry() changed it.

Returns int

public function getTransport(): ClientInterface

Returns the PSR-18 transport this client will use.

When HttpClientConfig::transport() was never called, TransportFactory::default() is asked for one and the result is memoised, so every request from this config shares a transport.

Returns ClientInterface

public function header(string $name, string $value): HttpClientConfig

A default header sent with every request unless the request already sets it.

ParameterTypeDescription
$namestring
$valuestring

Returns HttpClientConfig

public function headers(array<string, string> $headers): HttpClientConfig

ParameterTypeDescription
$headersarray``<``string``, ``string``>

Returns HttpClientConfig

public function retry(int $attempts, int $baseDelayMs = 100): HttpClientConfig

Retry transient failures (network errors, HTTP 429, HTTP 5xx) up to $attempts extra times, with exponential backoff from $baseDelayMs.

ParameterTypeDescription
$attemptsint
$baseDelayMsint

Returns HttpClientConfig

public function transport(ClientInterface $transport): HttpClientConfig

Override the underlying PSR-18 transport for this client (default: TransportFactory::default()).

ParameterTypeDescription
$transportClientInterface

Returns HttpClientConfig