Monitor HTTP Interactions with Laravel's New Http::record() Method
Last updated on by Harris Raftopoulos

Laravel introduces a powerful new tool for HTTP debugging with the Http::record() method, allowing you to inspect real HTTP requests while maintaining actual external service communication.
use Illuminate\Support\Facades\Http; // Start recording HTTP interactionsHttp::record(); // Make actual HTTP requestsHttp::get('https://example.com/api/users');Http::post('https://example.com/api/orders', ['product_id' => 123]); // Access the recorded requests and responsesHttp::recorded(function ($request, $response) { // Analyze request/response details});
Unlike Http::fake() which prevents actual external communication, this method gives you visibility into real requests while letting them execute normally.
public function testProductApiIntegration(){ // Start recording HTTP interactions Http::record(); // Perform the actual operation that makes API calls $result = $this->app->make(ProductService::class)->syncWithExternalApi(); // Verify the operation was successful $this->assertTrue($result); // Now verify the HTTP interactions $apiCallCount = 0; $allStatusCodesSuccessful = true; Http::recorded(function ($request, $response) use (&$apiCallCount, &$allStatusCodesSuccessful) { $apiCallCount++; // Check response status if ($response->status() >= 400) { $allStatusCodesSuccessful = false; } // Log the interaction for debugging Log::debug('API Call', [ 'url' => $request->url(), 'method' => $request->method(), 'status' => $response->status() ]); }); $this->assertGreaterThan(0, $apiCallCount, 'No API calls were made'); $this->assertTrue($allStatusCodesSuccessful, 'Some API calls failed');}
This approach is invaluable for integration testing, debugging external API communications, auditing API usage, and monitoring third-party service performance - all without interrupting the normal flow of your application. By allowing inspection without modification, Http::record() fills an important gap between fully mocked tests and unmonitored live API interactions.