Skip to content

Conversation

@faisuc
Copy link
Contributor

@faisuc faisuc commented Nov 23, 2025

When modifying the Accept header in middleware using $request->headers->set('Accept', 'application/json'), methods like wantsJson(), acceptsJson(), and prefers() would return stale values because Symfony's getAcceptableContentTypes() caches the result and doesn't clear it when headers are changed.

This caused issues in middleware scenarios like:

if ($request->acceptsAnyContentType()) {
    $request->headers->set('Accept', 'application/json');
}
if (!$request->wantsJson()) {
    return response()->json(['message' => 'Not Acceptable.'], 406);
}

Even after setting the Accept header to application/json, wantsJson() would still return false because it was using the cached value from before the header change.

Fixes: #57868

@faisuc faisuc marked this pull request as draft November 23, 2025 20:23
@faisuc faisuc marked this pull request as ready for review November 23, 2025 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request wantsJson operates on cached acceptableContentTypes

1 participant