Laravel 9 HTTP Guzzle Client Request Example

Hello Artisan,

In this tutorial, I am going to show you laravel 9 http client request example. You know when we need to work with api request, then we need http client support to make our request successful. Laravel has a beautiful package to do that.

It is good to see that the version of Laravel 9, Laravel provides an inbuilt HTTP Client using guzzlehttp/guzzle package. So we can easily run HTTP client requests using HTTP facade.

From this HTTP client request, We can send GET, POST, PUT, DELETE requests with you can easily get responses with text and JSON too. We can also pass header and authentication tokens easily. 

This tutorial will give you a simple example of how to use php laravel 9 HTTP client request. We will do the following things for laravel 9 http Client post request. So let's see the example of how to use Laravel HTTP client request or Guzzle HTTP client request in Laravel application.

We will complete the below request in this tutorial:

  • Laravel 9 HTTP cURL GET Request Example
  • Laravel 9 HTTP cURL POST Request Example
  • Laravel 9 HTTP cURL PUT Request Example
  • Laravel 9 HTTP cURL DELETE Request Example

 

laravel-9-guzzle-http-client-request-example

 

Step 1 : Create Route

Now we need to create routes to test our api using Laravel 9 HTTP guzzlehttp/guzzle client.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\PostController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('posts', [PostController::class, 'index']);
Route::get('posts/store', [PostController::class, 'store']);
Route::get('posts/update', [PostController::class, 'update']);
Route::get('posts/delete', [PostController::class, 'delete']);

 

Step 2: Create Controller

Now we need a post controller to write all of those routes methods to check HTTP client requests in Laravel 9. So create a PostController in the following path and update like:

app/Http/Controllers/PostController.php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

class PostController extends Controller
{
        public function index()
        {
            $response = Http::get('https://jsonplaceholder.typicode.com/posts');
        
            $jsonData = $response->json();
            
            dd($jsonData);
        }

        public function store()
        {
            $response = Http::post('https://jsonplaceholder.typicode.com/posts', [
                        'title' => 'This is test from ItSolutionStuff.com',
                        'body' => 'This is test from ItSolutionStuff.com as body',
                    ]);
      
            $jsonData = $response->json();
          
            dd($jsonData);
        }

        public function update()
        {
            $response = Http::put('https://jsonplaceholder.typicode.com/posts/1', [
                        'title' => 'This is test from ItSolutionStuff.com',
                        'body' => 'This is test from ItSolutionStuff.com as body',
                    ]);
      
            $jsonData = $response->json();
          
            dd($jsonData);
        }

        public function delete()
        {
            $response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');
      
            $jsonData = $response->json();
          
            dd($jsonData);
        }
}

 

Read also: Async HTTP client Requests are Coming in Laravel 8.x

 

Now hope you know how to use guzzle HTTP client requests in your laravel 9 application. Hope this tutorial will help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js