spatie/guzzle-redirect-history-middleware
Guzzle middleware that records redirect chains for each request. Attach RedirectHistoryMiddleware to your handler stack to capture every Location hop and inspect the final URL and intermediate redirects via a RedirectHistory instance.
Http::client()) or custom Guzzle stacks. No breaking changes expected given Guzzle’s stable API.Http::macro('withMiddleware', ...)).Http::withMiddleware()).GuzzleHttp\Psr7\Request/Response changes.Http::history() (if using Http::macro) suffice?monolog/handler for structured logging.Http::fake().)Http::withMiddleware() or global middleware.
// Global middleware (app/Providers/AppServiceProvider.php)
public function boot(): void
{
Http::macro('withRedirectHistory', function () {
return Http::withMiddleware(function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$middleware = new \Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistoryMiddleware();
return $middleware->process($request, $options, $handler);
};
});
});
}
$client = new Client([
'middleware' => [
new \Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistoryMiddleware(),
],
]);
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Middleware throws exception | HTTP request fails | Wrap in try-catch or use Guzzle’s error handling. |
| Redirect loop (infinite) | High CPU/memory usage | Set max_redirects in Guzzle config. |
| Guzzle version incompatibility | Middleware fails silently | Test with target Guzzle version early. |
| Logging overload | High disk I/O (if logging all) | Filter redirects (e.g., only 3xx codes). |
How can I help you explore Laravel packages today?