Skip to content

Request wantsJson operates on cached acceptableContentTypes #57868

@smares

Description

@smares

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 */*.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions