How to Get Request Header in Laravel 10

Websolutionstuff | Dec-11-2023 | Categories : Laravel

Ever wondered how to access request headers in your Laravel 10 application? Join me as I guide you through a quick and simple step-by-step process. In this tutorial, we'll create an endpoint to effortlessly retrieve all headers or specific ones.

In this article, I'll show you how to get a request header in laravel 10. Also, you can use this example in Laravel 7, Laravel 8, and Laravel 9.

Let's dive into the world of Laravel and explore the power of handling request headers effortlessly. Also, I use the header() method of request.

Step 1: Create a Route

Define a route in your routes/web.php file or routes/api.php file, depending on whether you are working with a web or API route.

use App\Http\Controllers\UserController;

Route::get('/get-request-headers', [UserController::class, 'getRequestHeaders']);

 

Step 2: Create a Controller

Generate a controller if you don't have one already:

php artisan make:controller UserController

 

Step 3: Implement the Controller Method

Open the generated controller (located in the app/Http/Controllers directory) and implement the method to retrieve request headers:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class UserController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getRequestHeaders(Request $request)
    {
        $headers = $request->header(); /* Getting All Headers from request */
  
        $header = $request->header('Accept'); /* Getting Singe Header value from request */
    
        return response()->json($headers);
    }
}

 

Step 4: Test the Endpoint

Run your Laravel development server:

php artisan serve

 

Step 5: Retrieve Specific Header

If you want to retrieve a specific header, you can use the header method on the request:

public function getRequestHeaders(Request $request)
{
    // Retrieve a specific header
    $contentType = $request->header('Content-Type');

    return response()->json(['Content-Type' => $contentType]);
}

 

Step 6: Handle Missing Headers

To handle cases where a header may not exist, you can use the has method:

public function getRequestHeaders(Request $request)
{
    // Check if a header exists
    if ($request->headers->has('Authorization')) {
        $authorizationHeader = $request->header('Authorization');
        return response()->json(['Authorization' => $authorizationHeader]);
    } else {
        return response()->json(['message' => 'Authorization header not present'], 400);
    }
}

That's it! You've successfully created an endpoint to retrieve request headers in Laravel 10.

 


You might also like:

Recommended Post
Featured Post
How To Use Sweetalert2 In Laravel
How To Use Sweetalert2 In Lara...

Today we will learn how to use sweetalert2 In laravel, You can use sweetalert2 in laravel as well as php, ...

Read More

May-03-2021

How To Check Array Is Empty Or Null In Javascript
How To Check Array Is Empty Or...

In this example, I will show you how to check if an array is empty or null in javascript or jquery. When we are wor...

Read More

Aug-18-2020

Laravel 8 Group By Query Example
Laravel 8 Group By Query Examp...

In this example we will see laravel 8 group by query example. how to use group by in laravel 8. As you might expect...

Read More

Nov-29-2021

Laravel 9 Livewire Datatable Example
Laravel 9 Livewire Datatable E...

In this article, we will see the laravel 9 livewire datatable example. Here, we will learn how to use livewire data...

Read More

Nov-30-2022