Laravel 7 - Create HTTP requests to communicate with other websites

Larave is adding new features to its new versions and improving day by day. With the new Laravel 7.X version, there are lots of new features are added. In the latest version, Laravel has added expressive, minimal API to create guzzleHTTPrequest.

In this article, we will discuss on how to make http request to other websites using guzzle in Laravel. Laravel has introduced new facade Http which makes guzzleHttp request. With Http facade, you can use get, post, put, patch, and deete method.

Laravel uses Guzzle package to create request and By default, automatically included as dependency. You can also install Guzzle by composer.

composer require guzzlehttp/guzzle

So lets get into this with example.

Create simple get request.

First of all, make basic get request with bellow code. Don't forget to use Http facade.

use Illuminate\Support\Facades\Http;

$response = Http::get('');

The get method will return an instance of Illuminate\Http\Client\Response, which allows to call many method to get data.

$response->body() : string;
$response->json() : array;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;

So if you want to get html code of, just create new get request as bellow. It will return as string data.

use Illuminate\Support\Facades\Http;

$response = Http::get('');
$response_html = $response->body();

Send request with data

You may also create post, put or patch method with additional data. By default, data will be sent as application/json content type:

$response = Http::post('', [
    'name' => 'Steve',
    'body' => 'Thank you very much',

If you want to send data as application/x-www-form-urlencoded  content type request instead of application/json content type, use asForm() method instead of post() method.

$response = Http::asForm('', [
    'name' => 'Steve',
    'body' => 'Thank you very much',

Send request with file

You may also want to send file with request. Call attach() method before calling request. The attach() method accepts two required parameters, name of the file and content of file.

$response = Http::attach(
    'profile', file_get_contents('photo.jpg'), 'photo.jpg'

Add headers

To add more headers to request, attach withHeaders() method before request call.

$response = Http::withHeaders([
    'private_key' => '7YlIdcrjMt9qFU29klgonl4s5gd5sbsd578g'
])->post('', [
    'name' => 'John',


If your request needs authorization, you may specify basic and digest authentication credentials using the withBasicAuth and withDigestAuth methods.

Basic authentication

$response = Http::withBasicAuth('[email protected]', 'password')->post('');

Digest authentication

$response = Http::withDigestAuth('[email protected]', 'password')->post('');

Bearer Tokens

If request need to add Authorization of bearer token in header, you can simply attach withToken() method.

$response = Http::withToken('v58sfsef8bsd24wf5sefs8fgvsds

Timeout request

You may add timeout() method to wait for request for specified number of seconds.

$response = Http::timeout(10)->get('');

After timeout, an instance of Illuminate\Http\Client\ConnectionException will be thrown.


If you need to create automatic retries, you may add retry() method. The retry() method accepts two parameters, number of times request retries and number of milliseconds wait for new request.

$response = Http::retry(5, 100)->post('');

After all requests failed, an instance of Illuminate\Http\Client\RequestException will be thrown.

Error handling

You can check if request returned with any error.

The successful() method will return true if request is ok.


The clientError() method will return true if there is client side error occured.


The serverError() method will return true if there is server error occured.


If you want to throw error response, use throw() method.

$response = Http::post('');

This way you can create request in Laravel 7 application. If you have any suggetion or question, please write in the bellow comment section. Also follow us on Twitter or like our Facebook page.