How to Get Request Header in Laravel?
Hello Friends,
This tutorial shows you how to get request header value in laravel. Here you will learn how to get request header in laravel. I’m going to show you about laravel get all headers from request. you'll learn laravel get header from request.
You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.
Sometimes, we need to get headers value from request in the laravel application. in this example, i will give you very simple example of getting request header using header() method of request.
So, let's see the below example:
Step 1: Create Route
In this step, we will add one api/posts route to return all headers from request in laravel. So, let's add a new route to that file.
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('api/posts', [PostController::class, 'index']);
Step 2: Create Controller
in the next step, now we have created a new controller as PostController and write index method on it like as below, So let's create a controller:
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$headers = $request->header(); /* Getting All Headers from request */
$header = $request->header('Accept'); /* Getting Singe Header value from request */
return response()->json($headers);
}
}
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web postman, type the given URL and view the app output:
http://localhost:8000/api/posts
Postman Output:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get All Routes in Laravel?
- Laravel Download File from URL to Storage Example
- How to Write Text on Existing PDF File in Laravel?
- How to Truncate String in Laravel?
- How to Convert Image to Base64 in Laravel?
- How to Get Browser Name and Version in Laravel?
- How to Convert JSON to Array in Laravel?
- Laravel Money/Currency Format Example
- Laravel Cashier Stripe Subscription Example Tutorial
- How to Use Google Translator in Laravel?
- How to Select Specific Columns in Laravel Eloquent Model?
- How to Get All Models in Laravel?