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.
HttpClient facade or custom Guzzle clients).register().HttpClient with a custom macro or decorator.request.sent, response.received) to process history data.array_merge behavior changes).HttpClient) is recommended.tap() methods or custom middleware achieve the same goals with less risk?HttpClient::withMiddleware() or service provider binding.
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(GuzzleHttp\Client::class, function ($app) {
$client = new GuzzleHttp\Client();
$client->getEmitter()->attach(
new \Csa\GuzzleHistoryMiddleware\HistoryMiddleware(
new \Csa\GuzzleHistoryMiddleware\Storage\ArrayStorage() // Replace with custom storage
)
);
return $client;
});
}
new GuzzleHttp\Client()), attach the middleware directly.Illuminate\Http\Client\Events\RequestSent/ResponseReceived and processing history data.StorageInterface) to persist history to:
request_history table with JSON columns).class DatabaseStorage implements StorageInterface {
public function addRequest(RequestInterface $request): void {
DB::table('request_history')->insert([
'method' => $request->getMethod(),
'uri' => $request->getUri(),
'headers' => $request->getHeaders(),
'body' => $request->getBody()->getContents(),
'created_at' => now(),
]);
}
}
RequestInterface/ResponseInterface updates, middleware attachment API.getEmitter().illuminate/http-guzzle (Laravel’s Guzzle wrapper).HttpClient facade methods (e.g., asForm, withOptions).Authorization, password).composer.json to avoid unexpected updates.// app/Console/Commands/DumpHistory.php
public function handle() {
$history = app(\Csa\GuzzleHistoryMiddleware\Storage\ArrayStorage::class)->getHistory();
dd($history);
}
How can I help you explore Laravel packages today?