What is the HTTP client?
Laravel’s HTTP client wraps Guzzle with a concise, expressive API. Access it through theHttp facade to send requests to external web services.
Guzzle is included with Laravel, so no extra installation is required.
Making requests
GET
POST
Data is sent asapplication/json by default:
PUT, PATCH, DELETE
Handling responses
Every request method returns anIlluminate\Http\Client\Response instance:
Request options
Headers
For browser-side AJAX requests to your Laravel app (not outbound server-to-server calls), see CSRF protection for
X-CSRF-TOKEN and X-XSRF-TOKEN usage.Authentication
Base URL
When you make multiple requests to the same host, set a base URL once:Form data
Send data asapplication/x-www-form-urlencoded:
Timeouts
Retries
Automatically retry failed requests:Error handling
The HTTP client does not throw an exception for 4xx or 5xx responses by default. Check the status manually:Throwing on error
Callthrow() to turn an error response into an Illuminate\Http\Client\RequestException:
throw() returns the response instance, you can chain further calls:
Concurrent requests
Send multiple requests simultaneously withpool():
Testing
Faking responses
UseHttp::fake() to intercept requests in tests without hitting real URLs:
Asserting requests
Verify that your code sent the expected requests:Service class example
Encapsulate HTTP client logic in a dedicated service class for better testability and reuse.1
Create a service class
2
Register in a service provider
3
Use in a controller
4
Write a test
Quick reference
Request methods
Request methods
Response status helpers
Response status helpers
Best practices
Best practices
- Wrap HTTP client logic in a service class, not directly in controllers.
- Always set a timeout. External APIs can hang indefinitely without one.
- Use
retry()for transient errors such as network blips or rate limiting. - Call
Http::fake()at the start of every test that involves HTTP calls. - Add
Http::preventStrayRequests()to your test setup to catch accidental real requests. - Store API tokens in environment variables and reference them through
config/services.php.