Laravel Version
12.39.0
PHP Version
8.4.14
Database Driver & Version
No response
Description
Request::wantsJson uses Symfony's getAcceptableContentTypes internally which caches the headers so this doesn't work in a middleware:
if ($request->acceptsAnyContentType()) {
$request->headers->set('Accept', 'application/json');
}
if (!$request->wantsJson()) {
return response()->json(['message' => 'Not Acceptable.'], 406);
}
return $next($request);
There is also no way to trigger a reset of acceptableContentTypes.
Steps To Reproduce
Have the following code in a middleware:
if ($request->acceptsAnyContentType()) {
$request->headers->set('Accept', 'application/json');
}
dd($request->wantsJson());
Call an endpoint with Accept set to */*.