csa/guzzle-history-middleware
Capture and inspect Guzzle HTTP request/response history with a middleware you can drop into your handler stack. Useful for testing, debugging, and logging, with upgrade notes and contribution guidelines included.
Installation:
composer require csa/guzzle-history-middleware
Add the middleware to your Guzzle client stack:
use CSA\GuzzleHistoryMiddleware\HistoryMiddleware;
use GuzzleHttp\HandlerStack;
$stack = HandlerStack::create();
$stack->push(HistoryMiddleware::history());
$client = new Client(['handler' => $stack]);
First Use Case: Track HTTP request history for debugging or auditing:
$response = $client->get('https://api.example.com/data');
$history = HistoryMiddleware::getHistory(); // Retrieve recorded requests/responses
HistoryMiddleware::history() to create a recordable middleware.HistoryMiddleware::getHistory() to fetch stored requests/responses.HistoryMiddleware::clearHistory() to reset the history.Middleware Integration:
$stack = HandlerStack::create();
$stack->push(HistoryMiddleware::history(), 'history'); // Named middleware for easy removal
Request/Response Logging: Automatically captures:
Laravel-Specific Integration:
// In a Laravel service provider
$this->app->singleton(GuzzleClient::class, function ($app) {
$stack = HandlerStack::create();
$stack->push(HistoryMiddleware::history());
return new Client(['handler' => $stack]);
});
$middleware = HistoryMiddleware::history(['enabled' => request()->has('debug')]);
$history = HistoryMiddleware::getHistory();
HistoryRepository::save($history);
$stack->remove('history'); // Disable history when no longer needed
$response = $client->post('/api/endpoint', ['json' => $data]);
dd(HistoryMiddleware::getHistory()); // Inspect last request
$history = HistoryMiddleware::getHistory();
$this->assertCount(1, $history);
event(new RequestLogged(HistoryMiddleware::getHistory()));
Thread Safety:
Memory Leaks:
HistoryMiddleware::clearHistory();
Middleware Order:
history middleware before retry/redirect middleware to capture original requests.[retry, history] → Retried requests may appear as separate entries.[history, retry].Sensitive Data:
$middleware = HistoryMiddleware::history(['sanitize' => true]);
Empty History:
Incomplete Requests:
GuzzleHttp\Psr7\Stream objects are being converted to strings. Use:
HistoryMiddleware::history(['stream_as_string' => true]);
Performance Overhead:
$middleware = HistoryMiddleware::history(['enabled' => app()->isLocal()]);
Custom History Storage: Override the default static storage:
HistoryMiddleware::setHistoryStorage(new ArrayHistoryStorage());
Filtering History:
$filtered = array_filter(HistoryMiddleware::getHistory(), function ($entry) {
return str_contains($entry['request']['uri'], 'api/v1');
});
Event Dispatching: Trigger events on request completion:
$middleware = HistoryMiddleware::history([
'on_complete' => function ($history) {
event(new RequestCompleted($history));
}
]);
Service Container Binding: Bind the client with history middleware in a provider:
$this->app->bind(GuzzleClient::class, function ($app) {
$stack = HandlerStack::create();
$stack->push(HistoryMiddleware::history());
return new Client(['handler' => $stack]);
});
Queue Workers: History middleware may interfere with queued jobs. Clear history after processing:
dispatch(new ProcessOrder($order))->after(function () {
HistoryMiddleware::clearHistory();
});
Testing: Reset history between tests:
public function setUp(): void {
parent::setUp();
HistoryMiddleware::clearHistory();
}
How can I help you explore Laravel packages today?