-
Notifications
You must be signed in to change notification settings - Fork 11.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[10.x] Add global middleware to Http
client
#47525
Merged
Merged
Conversation
This file contains 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
898e482
to
30552f8
Compare
Http
factory
30552f8
to
046b72b
Compare
Http
factoryHttp
client
1 task
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.
💡 Updated!
This PR allows an application to add Guzzle middleware that is applied to every request made via the
Http
client. It also introduces some nicer named methods to add request / response only middleware.Read more about Guzzle middleware in their docs.
As an example, an application may globally set a
User-Agent
header, to be a good citizen of the Internet. To do this, you may add a global "request" middleware:This will mean that every outgoing request will now have the
User-Agent: Laravel Framework/1.0
header.Here is another example, showing the addition of a global "response" middleware.
It is also possible, of course, to add a complete middleware that wraps both the request and response cycle. As an example, the following middleware will add a response header including the duration of the request in milliseconds. The value may also be logged or recorded elsewhere.
Something like the above would also be possible via the
on_stats
Guzzle option.The global middleware is also useful for logging and inspection of outgoing requests or incoming responses, although this is also possible by listening to events, but now we have a unified way of doing this.
I've also introduced named middleware methods for the request / response middleware to the
PendingRequest
class. These make adding middleware feel more at home in Laravel, IMO, when compared to having to reference theGuzzleHttp\Middleware
class directly.Here is a before and after of using the new named methods for a "request" middleware:
You can see the currently documented way in the screenshot below. I feel these named methods greatly streamline the user-facing API.
Here is the full API for adding middleware to Http requests after this PR:
Similar PRs:
Http::configure()
method #40332