Laravel 7 Http facade

Laravel 7 Http facade


Laravel 7x included an Http facade for making Http request from server-side. Before Laravel 7, most of the developers used Guzzle Http client package for making Http request which is not easy for the beginner and a little bit complex when working on API request, authentication and so on. From Laravel 7, developers can use HTTP facade for making an HTTP request with nice and cleaner syntax very easily! Actually, the Laravel 7 Http facade use Guzzle library behind the sense but the Http facade class comes with a very easy and cleaner syntax which makes it more elegant for developers. Let's explore the Laravel 7 Http facade with example.

For Laravel 7 Http facade, we have to include the statement in our code.

use Illuminate\Support\Facades\Http;

 

Laravel 7 Http GET request

It's very simple to make Http get request with Laravel 7 Http facade. We have to just pass the URL in the get method and the rest of the things will handle by Http facade. We can parse the response body with the JSON method, which makes life easier, isn't it?

$response = Http::get("http://jsonplaceholder.typicode.com/todos/1");
$response->json();

laravel7-http-response.png

We can get the response data in a various way by these methods

$response['foo']; // get the specific data from body
$response->body(); // it will return raw response body data
$response->json(); // it will parse response body to JSON
$response->headers(); // get the response headers

 

GET request with a parameter

We have to pass another associative array in the get method for passing the parameter. Let's take an example.

$response = Http::get("http://example.com/search",['catId' => 123]);

This will make the get request like http://example.com/search?catId=123

 

Laravel 7 Http POST request

Making POST request like as the GET request which we have learned earlier.

$data = [
  'email'=>'user@example.com',
  'password'=>12345
];
$response = Http::post("http://example.com/api/login",$data);

if($response->ok()){
  // do whatever you want
}

POST request with headers

Sometimes we need to pass headers information with our request. In Laravel 7 Http facade we can easily do that by the withHeaders method.

$data = [
  'title'=>'Task No 1',
  'status'=>1
];
$url = "http://example.com/api/todos/create";

$response = Http::withHeaders(['token' => 'xe234232...'])->post($url,$data);
if($response->ok()){
  // do whatever you want
}

 

Laravel 7 Http request helpers

// send as a form-data
$response = Http::asForm()->post(...);

//send raw body
$response = Http::withBody(base64_encode($photo), 'image/jpeg')->post(...);

$response = Http::withHeaders(['foo'=>'bar'])->post...); // send headers with request

//send files as multi-part
$response = Http::attach('attachment',file_get_contents('photo.jpg'))->post(...);

// basic authentication
$response = Http::withBasicAuth('user@example.com', 'q2343')->post(...);

$response = Http::withToken('q23432x342')->post(...);

 

Laravel 7 Http response helpers

Here are some laravel 7 Http facade response helper which help us deal with our response.

$response->status();
$response->ok();
$response->successful();
$response->serverError();
$response->clientError();

 

Laravel 7 Http timing helpers

//wait maximum of 3 seconds for the response
Http::timeout(3)->get(...);

//3 times retry with 100 milliseconds interval
Http::retry(3, 100)->post(...);
 

Share on


sidebar ad


Related Post


Laravel Barcode generation tutorial

Laravel PDF generation tutorial

Laravel Validation Cheat Sheet

PEST - Make Laravel test easier

Laravel Google reCaptcha without package

Speed Up Laravel website - Step by Step