Laravel Tutorials

Monitor HTTP Interactions with Laravel's New Http::record() Method

Published
Monitor HTTP Interactions with Laravel's New Http::record() Method image

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 interactions
Http::record();

// Make actual HTTP requests
Http::get('https://example.com/api/users');
Http::post('https://example.com/api/orders', ['product_id' => 123]);

// Access the recorded requests and responses
Http::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.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article