Category : #laravel
Tags : #laravel, #laravel api
In the previous tutorial, we did see how to send a POST request using the Laravel Guzzle HTTP client. But in this tutorial, I will discuss how to set headers with attachments with multipart/form-data using the Guzzle HTTP client. We will use Http client to create this test API request with headers and attachments.
You already know that Laravel provides an expressive, minimal API around the Guzzle HTTP client, allowing us to make HTTP requests to communicate with other web applications
So let's see the example code of laravel http client set headers:
Step 1: Install Laravel 10
First of all, we need to get a fresh Laravel 10 version application using the bellow command to start multipartform data guzzle laravel.
composer create-project laravel/laravel example-app
Step 2: Create Route
Here, we need to add routes to set laravel http client set headers.
routes/web.php
<?php
use App\Http\Controllers\HttpController;
use Illuminate\Support\Facades\Route;
Route::post('http_post_request', [HttpController::class, 'store']);
Step 3: Create Controller
Here, now we have to create a HttpController to process our api request. So create it and update it like below:
app/Http/Controllers/HttpController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class HttpController extends Controller
{
public function store(Request $request)
{
$response = Http::withHeaders([
'X-First' => 'Laravelia',
'X-Second' => 'CodeCheef'
])->post('http://example.com/users', [
'name' => 'Mahedi Hasan',
]);
dd($response);
}
}
Now if you start your server by running php artisan serve
and visit the below url:
URL
Sometimes we need to send data with attachments. So if you have multipartform data guzzle laravel, then you can follow this code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class HttpController extends Controller
{
public function store(Request $request)
{
$photo = fopen(public_path('/storage/') . $photoPath, 'r');
$response = Http::withToken($token)
->withHeaders([
'X-Header' => $header
])
->attach('photo', $photo)
->post($url, [
[
'name' => 'param_1',
'contents' => 'contents'
]
]);
dd($response);
}
}
For only attachment, you can follow this code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class HttpController extends Controller
{
public function store(Request $request)
{
$photo = fopen('photo.jpg', 'r');
$response = Http::attach(
'attachment', $photo, 'photo.jpg'
)->post('http://example.com/attachments');
dd($response);
}
}
Or you can use this code for multipart form data:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class HttpController extends Controller
{
public function store(Request $request)
{
$response = Http::attach(
'attachment', file_get_contents('photo.jpg'), 'photo.jpg'
)->post('http://example.com/attachments');
dd($response);
}
}
Read also: Laravel 10 Guzzle HTTP Client POST Request Example
Conclusion
You know how to use laravel http client multipartform data. Hope after following this laravel http client set headers example, you own now be able to set laravel http client default headers. Hope this multipartform data guzzle laravel tutorial will help you.