Intermediate LevelLaravel
How does the Http facade provide a fluent wrapper over Guzzle for external API requests?
❓ Interview Prompt: Explain Http facade, Http::get(), Http::post(), Http::retry(), Http::timeout(), and fake HTTP testing (Http::fake()).
Laravel's Http facade provides an expressive wrapper around Guzzle for making outbound HTTP API requests:
Key Methods:
•
'Http::timeout(5)->get("...")': Sets request timeout bounds.
•
'Http::retry(3, 100)->post("...")': Retries failed requests up to 3 times with exponential backoff.
•
*'Http::fake(["api.com/" => Http::response(["status" => "ok"], 200)])'**: Mocks API responses instantly in unit tests!
Code Implementation Example
php
<?php
use IlluminateSupportFacadesHttp;
// Outbound API request with retry logic and error throwing
$response = Http::retry(3, 200)
->timeout(5)
->withToken($apiToken)
->get('https://api.thirdparty.com/v1/data');
if ($response->successful()) {
$data = $response->json();
}💡Making outbound API request using Http facade with retry and timeout.
🎯 Interview Key Takeaway
Use the Http facade for outbound requests, leveraging Http::retry() for resilience and Http::fake() for fast testing.
#Http Facade#Guzzle#API Requests#Intermediate
Back to all Laravel Questions→