HTTP Client
Symfony's built-in HTTP client. Async by default, supports streaming, retries, and easy mocking in tests.
- Inject `HttpClientInterface` and call `$client->request('GET', $url, [...])`.
- Responses are lazy — the request only fires when you call `getStatusCode()`, `getContent()`, or `toArray()`.
- Use `$client->stream([$response1, $response2])` to fan out multiple requests in parallel.
- Scope clients per service via `framework.http_client.scoped_clients` to bake in base URI, auth headers, retry policy.
- In tests, use `MockHttpClient` to script responses — no real HTTP traffic.
- Decorators like `RetryableHttpClient` and `RateLimiterHttpClient` add cross-cutting behavior.
$response = $client->request('GET', 'https://api.example.com/users', [
'auth_bearer' => $token,
'timeout' => 5,
]);
$users = $response->toArray(); // throws on non-2xx
Common gotchas
- If you do not consume the response, the request never fires — easy bug when fire-and-forget. Call `getStatusCode()` to force it.
- Default timeout is 0 (no timeout). Always set a timeout for outbound requests in production.