# Laravel 9 Guzzle Http Request Example, Http::get,Http::post,ttp::put, Http::delete (ok)

C:\xampp\htdocs\test\routes\web.php

```
<?php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
  return view('welcome');
});
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']);
```

C:\xampp\htdocs\test\app\Http\Controllers\PostController.php

```
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
class PostController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function index() {
    $response = Http::get('http://jsonplaceholder.typicode.com/posts');
    $jsonData = $response->json();
    echo "<pre> status:";
    print_r($response->status());
    echo "<br/> ok:";
    print_r($response->ok());
    echo "<br/> successful:";
    print_r($response->successful());
    echo "<br/> serverError:";
    print_r($response->serverError());
    echo "<br/> clientError:";
    print_r($response->clientError());
    echo "<br/> headers:";
    print_r($response->headers());
  }
  /**
   * Write code on Method
   *
   * @return response()
   */
  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);
  }
  /**
   * Write code on Method
   *
   * @return response()
   */
  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);
  }
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function delete() {
    $response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');
    $jsonData = $response->json();
    dd($jsonData);
  }
}
```

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2Fmaedbzp29CvH1vYdZUAe%2FScreenshot_6.png?alt=media\&token=199a900a-6018-46b6-8a17-7f1bac9f40b8)

\
Hi,

Here, I will show you how to work laravel 9 http request example. if you want to see an example of laravel 9 http client request example then you are the right place. step by step explain laravel 9 guzzle http client example. This tutorial will give you a simple example of php laravel 9 http client request. you will do the following things for laravel 9 http Client post request.

Laravel 9 provides an inbuilt HTTP Client using guzzlehttp/guzzle package. you can easily run HTTP client requests using HTTP facade. you can send GET, POST, PUT, DELETE requests with you can easily get responses with text and JSON too. you can also pass header and authentication tokens easily.

Here, I will give you very simple examples of how to run a call HTTP API request from laravel 9?

1\) Laravel 9 HTTP cURL GET Request Example

2\) Laravel 9 HTTP cURL POST Request Example

3\) Laravel 9 HTTP cURL PUT Request Example

4\) Laravel 9 HTTP cURL DELETE Request Example

5\) Laravel 9 API with Response

Let's see one by one example

![](https://www.itsolutionstuff.com/upload/laravel-9-http-request.png)

**Install Laravel 9:**

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

```
composer create-project laravel/laravel example-app
```

**1) Laravel 9 HTTP cURL GET Request Example:**

Here, we will see how to send curl http get request in laravel 9, let's update route file code and controller file code. you can see output as well:

routes/web.php

```
<?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']);
```

app/Http/Controllers/PostController.php

```
<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;use Illuminate\Support\Facades\Http;  class PostController extends Controller{    /**     * Write code on Method     *     * @return response()     */    public function index()    {        $response = Http::get('https://jsonplaceholder.typicode.com/posts');            $jsonData = $response->json();                  dd($jsonData);    }}
```

Output:

![](https://www.itsolutionstuff.com/upload/laravel-9-http-get-request.png)

**2) Laravel 9 HTTP cURL POST Request Example:**

Here, we will see how to send curl http post request in laravel 9, let's update route file code and controller file code. you can see output as well:

routes/web.php

```
<?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/store', [PostController::class, 'store']);
```

app/Http/Controllers/PostController.php

```
<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;use Illuminate\Support\Facades\Http;  class PostController extends Controller{    /**     * Write code on Method     *     * @return response()     */    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);    }}
```

Output:

![](https://www.itsolutionstuff.com/upload/laravel-9-http-post-request.png)

**3) Laravel 9 HTTP cURL PUT Request Example:**

Here, we will see how to send curl http put request in laravel 9, let's update route file code and controller file code. you can see output as well:

routes/web.php

```
<?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/update', [PostController::class, 'update']);
```

app/Http/Controllers/PostController.php

```
<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;use Illuminate\Support\Facades\Http;  class PostController extends Controller{    /**     * Write code on Method     *     * @return response()     */    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);    }}
```

Output:

![](https://www.itsolutionstuff.com/upload/laravel-9-http-put-request.png)

**4) Laravel 9 HTTP cURL DELETE Request Example:**

Here, we will see how to send curl http delete request in laravel 9, let's update route file code and controller file code. you can see output as well:

routes/web.php

```
<?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/delete', [PostController::class, 'delete']);
```

app/Http/Controllers/PostController.php

```
<?php   namespace App\Http\Controllers;  use Illuminate\Http\Request;use Illuminate\Support\Facades\Http;  class PostController extends Controller{    /**     * Write code on Method     *     * @return response()     */    public function delete()    {        $response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');          $jsonData = $response->json();              dd($jsonData);    }}
```

**5) Laravel 9 API with Response:**

We will create very simple http request full example. we need to create simple route to call controller method. so let's create it:

routes/web.php

```
<?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']);
```

app/Http/Controllers/PostController.php

```
<?php  namespace App\Http\Controllers;  use Illuminate\Http\Request;use Illuminate\Support\Facades\Http; class PostController extends Controller{    public function index()    {    	$response = Http::get('http://jsonplaceholder.typicode.com/posts');      	$jsonData = $response->json();    	      	echo "<pre> status:";    	print_r($response->status());    	echo "<br/> ok:";    	print_r($response->ok());        echo "<br/> successful:";        print_r($response->successful());        echo "<br/> serverError:";        print_r($response->serverError());        echo "<br/> clientError:";        print_r($response->clientError());        echo "<br/> headers:";        print_r($response->headers());    }}
```

Output:

Read Also: [Laravel 9 Send Mail using Gmail SMTP Server](https://www.itsolutionstuff.com/post/laravel-9-send-mail-using-gmail-smtp-serverexample.html)

```
 status:200 ok:1 successful:1 serverError: clientError: headers:Array(    [Date] => Array        (            [0] => Thu, 12 Mar 2020 06:08:58 GMT        )    [Content-Type] => Array        (            [0] => application/json; charset=utf-8        )    [Transfer-Encoding] => Array        (            [0] => chunked        )    .....)
```

You can also get more information about Http Client in Laravel Docs: [Click Here](https://laravel.com/docs/9.x/http-client).

I hope it can help you...

\
