Skip to main content

What is the HTTP client?

Laravel’s HTTP client wraps Guzzle with a concise, expressive API. Access it through the Http 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 as application/json by default:

PUT, PATCH, DELETE

Handling responses

Every request method returns an Illuminate\Http\Client\Response instance:
JSON responses support array access:

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 as application/x-www-form-urlencoded:

Timeouts

Exceeding the timeout throws Illuminate\Http\Client\ConnectionException. Always set a timeout when calling external APIs.

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

Call throw() to turn an error response into an Illuminate\Http\Client\RequestException:
Because throw() returns the response instance, you can chain further calls:
Catch the exceptions separately:

Concurrent requests

Send multiple requests simultaneously with pool():
Concurrent requests are ideal for dashboards that aggregate data from multiple endpoints. See the Concurrency guide for running other types of concurrent tasks beyond HTTP requests.

Testing

Faking responses

Use Http::fake() to intercept requests in tests without hitting real URLs:
Return responses in sequence:

Asserting requests

Verify that your code sent the expected requests:
Add Http::preventStrayRequests() to your test setup. Any request that does not match a fake URL will throw an exception, catching accidental real HTTP calls.

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

  • 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.
Last modified on May 26, 2026