Introducing the new HTTP Client in Laravel
Laravel has introduced a new HTTP Client comes built-in with Laravel v7, It's built on top of Guzzle HTTP Client. In this short tutorial, we'll check out how this works out of the box in our Laravel apps.
Reading Time: < 1 minute
Laravel’s wrapper around Guzzle is focused on its most common use cases and a wonderful developer experience. For example, the client makes it a breeze to POST and interface with JSON data:
Post Request
use Illuminate\Support\Facades\Http;
$response = Http::withHeaders([
'Authentication' => 'Bearer $token'
])->post('http://test.com/users', [
'email' => 'info@thewebtier.com',
]);
return $response['id'];
Get Request
$response = Http::get($url);
$response = Http::get($url,['email'=>'info@thewebtier.com']);
Responses
Now with new HTTP Client, its even easier in getting responses instead of json_decode
and then retrieving the body as we used to in GuzzleHTTP.
$response['email']
$response->body()
$response->json()
$response->status()
$response->ok()
Comments