Technical Mid Level

How do you make HTTP requests in Symfony? Explain the HttpClient component, its configuration options, and how you would test code that makes external API calls.

Quick Tip

I define scoped clients in config for each external API. In tests, I inject MockHttpClient with predefined MockResponse objects — no network calls, deterministic results. In dev, I check the HttpClient profiler panel for request details.

What good answers include

Symfony HttpClient provides a PSR-18 compatible client with scoping (base_uri per named client), automatic JSON decoding, retry on failure, and async capabilities. Configure named clients in framework.yaml under http_client.scoped_clients. For testing: use MockHttpClient with MockResponse to simulate API responses without hitting real endpoints. Strong candidates discuss: the difference between streaming and buffered responses, using the retry_failed option with custom strategies, ScopedHttpClient for API-specific configuration, and CachingHttpClient for reducing external calls.

What interviewers are looking for

Tests how candidates handle external integrations. Those who use raw cURL or Guzzle in Symfony miss the framework integration. Those who cannot explain how to test external API calls will write untestable, fragile code.

← All Symfony questions