Skip to the stories
AI & ML

Symfony 8.2 HttpClient Adds Cache Controls, QUERY Support and HTTP/2 Prior Knowledge

  • Javier Eguiluz
  • 313 reads

Symfony 8.2 adds cache policies and QUERY caching, improves request encoding and retries, and changes HTTP/2 and downgrade redirect behavior.

Symfony 8.2 HttpClient Adds Cache Controls, QUERY Support and HTTP/2 Prior Knowledge

Symfony 8.2 makes the HttpClient component more configurable at the points where application behavior depends on the request or response: cache policy, request encoding, retries, protocol selection and buffering. It also changes two defaults with migration impact: HTTP/2 can now be forced from the first byte, and HTTPS redirects to HTTP are no longer followed automatically.

Nicolas Grekas

Cache responses with application rules

Symfony 7.4 updated the caching client to follow RFC 9111, but cache decisions were driven by response headers and cached entries couldn’t be tagged for targeted invalidation. Symfony 8.2 adds the extra.cache_policy option, a callable that receives a CachePolicy object, the response status code and headers. The callback can set expiration or attach tags, giving applications control over storage behavior for a request.

For example, a product response can be tagged as product-42 and assigned a one-hour lifetime. When the product changes, a tag-aware cache pool can invalidate responses carrying that tag. This gives teams a way to connect application data changes to cached HTTP responses without clearing the entire cache.

Pascal CESCON

When a tag depends on the response body, the policy can use tagFromBody(). That requires the request’s buffer option to be enabled; without buffering, the tags are ignored and Symfony logs a warning. The tradeoff is practical: body-based tagging needs the response content available to the cache logic, so applications should choose it only when response data is needed to determine invalidation groups.

Cache body-based QUERY requests

The caching client now supports HTTP QUERY requests alongside GET and HEAD. QUERY is safe and idempotent, like GET, but places query parameters in the request body rather than the URL. Previously, requests with bodies bypassed this cache.

Ayyoub AFW-ALLAH

Symfony includes both the body and its Content-Type in the cache key, so distinct query payloads are stored separately. Caching is bypassed if the request has no Content-Type header or if its body is not a string, such as a stream or closure. That boundary matters for clients that build request bodies dynamically: to benefit from caching, the query must have a stable string representation and an explicit media type.

Represent repeated fields directly

The query and body options can now accept a list of single-entry arrays, which allows repeated field names such as name=web&name=api. This format is common in HTTP APIs and matches the default array serialization used by OpenAPI 3. Before this change, Symfony encoded arrays using PHP-style indexed keys, such as name[0]=web&name[1]=api.

Grégoire Pineau

The list form also supports repeated body fields whose values are streams, making it possible to submit multiple files under one field name. Existing associative arrays continue to work. Values in the new list format must be scalars, Stringable objects, backed enums or null; null removes the parameter. Nested arrays raise an exception, so callers that need bracket notation must write the field name explicitly, for example tag[].

There is a compatibility change to review: a list whose first item is an array now means repeated fields. A value such as [['a' => 'b']], previously encoded as 0[a]=b, now produces a=b. Code relying on the old encoding should be adjusted before upgrading.

Benjamin Zaslavsky

Configure retry fallback hosts

RetryableHttpClient has been able to use a different base URI on each attempt since Symfony 6.3, which helps when an API is available through multiple hosts or mirrors. Symfony 8.2 makes this behavior configurable for scoped clients through retry_failed.base_uris. The listed URIs are tried after the scoped client’s primary base_uri, one for each retry attempt.

Options configured on the scoped client, including headers such as an API key, apply across all listed URIs. Applications that construct clients directly can use the new ScopingHttpClient::forBaseUris() method to scope a client to multiple base URIs.

HTTP/2 prior knowledge and redirects

For unencrypted HTTP/2 connections (h2c), including those used by gRPC servers, the server may expect HTTP/2 immediately. Previously, setting http_version to 2.0 on an HTTP URL could begin with an HTTP/1.1 request that asked to upgrade. In Symfony 8.2, that setting explicitly selects HTTP/2 from the start in both CurlHttpClient and AmpHttpClient.

This changes compatibility: a server that only speaks HTTP/1.1 cannot be reached when the option is set. The behavior also applies to HTTPS with AmpHttpClient or cURL 8.10 and later. Remove the explicit version setting when the client should negotiate the protocol.

Symfony also stops following redirects from HTTPS to HTTP. Following such a downgrade could send custom headers—and request bodies on 307 and 308 responses—over an unencrypted connection. The client now returns the 3xx response and exposes its destination through the redirect_url response info. Applications can inspect the target and make a separate request if they trust it. Other redirects, including HTTP to HTTPS, continue to be followed as before.

Buffering options reach the builder

The request-level buffer option already accepted a boolean, a stream resource, or a closure that decides based on response headers whether to buffer. In Symfony 8.2, HttpOptions::buffer() accepts all three forms as well, bringing the options builder into line with direct request configuration. A closure can, for example, buffer JSON responses based on their Content-Type.

Projects extending HttpOptions should update overrides of buffer() so the argument type changes from bool to mixed. Across these changes, the main upgrade work is checking code that pins HTTP/2, depends on automatic downgrade redirects, or passes lists of arrays to request options. Teams can then use the new cache and routing controls where those behaviors match their API contracts.

Source: Javier Eguiluz · symfony.com

From the same set