[13.x] Add prefersJsonResponses() to the application builder#59753
Merged
taylorotwell merged 4 commits into13.xfrom Apr 20, 2026
Merged
[13.x] Add prefersJsonResponses() to the application builder#59753taylorotwell merged 4 commits into13.xfrom
taylorotwell merged 4 commits into13.xfrom
Conversation
Contributor
|
It's funny you PR'd this today. I was just thinking today how every project I have worked on with Laravel needs this functionality and every project just had its own version of something like this. |
|
Nice idea. Would it make sense to apply it only on |
Member
Author
|
@cosmastech exactly, I already worked in many projects, each one doing its own way for this! I think this approach is simple enough to make it easy for everyone creating API-only apps! |
Member
Author
|
@rcerljenko the idea for this to be a global thing is that this is aimed at API-only Laravel apps |
Member
|
🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
API-first Laravel apps regularly hit a papercut: clients sending
Accept: */*(curl, most mobile SDKs, many HTTP libraries) get HTML redirects for auth failures, HTML error pages for unhandled exceptions, and HTML validation responses. Existing workarounds — custom middleware,setRequestFormat('json'), overriding the exception handler'sshouldReturnJson— all work, but none are a one-liner.This adds an opt-in
Application::configure()->prefersJsonResponses()on the application builder. When enabled, it prepends aPrefersJsonResponsesmiddleware to the global HTTP stack which rewritesAccepttoapplication/jsonwhen the incoming header expresses no specific preference (missing, empty, or every listed entry is*/*orapplication/*). Mixed lists that include a specific media-type are left alone so the client's preference still wins. The original header value is preserved onX-Original-Accept.Default behavior is unchanged — this only activates when you call it.
Example
Enable it in
bootstrap/app.php:With that one call in place, a broad
Acceptheader is treated as JSON:Pass
falseto disable conditionally (e.g. based on environment):The original
Acceptvalue is preserved onX-Original-Acceptin case you need it for logging or observability.