How to get Headers Data From Incoming Requests in Laravel?

Last updated on December 29, 2022

Laravel provides great features that help us to create API. Headers are the part of the API that carries information from the user end to the framework. In this article, we will learn how to get header data from incoming requests.

If you haven’t installed the Laravel framework yet you can check out our article that helps you to install fresh Laravel applications.

Once you set up a Laravel application then open your terminal and create a UserController. In UserController we will be getting header data, so run the below command in your terminal to create UserController.

php artisan make:controller UserController

Once the controller is created, then import the request class into it.

use Illuminate\Http\Request;

Next, create a userInfo method in which the header will be accessible.

public function userInfo(Request $request)
{
    $request->header();
}

The above userInfo() method has a header() function that returns all headers from incoming requests. Let’s use Laravel’s dd() method that dumps the provided argument and dies.

dd($request->header());

It returns all the headers. If you use dd() method over header() function same as above you will get the below response

array:8 [
  "user-agent" => array:1 [
    0 => "PostmanRuntime/7.29.0"
  ]
  "accept" => array:1 [
    0 => "*/*"
  ]
  "cache-control" => array:1 [
    0 => "no-cache"
  ]
  "postman-token" => array:1 [
    0 => "b9b7bb98-45bc-498e-bc7e-eca853bc1654"
  ]
  "host" => array:1 [
    0 => "127.0.0.1:8000"
  ]
  "accept-encoding" => array:1 [
    0 => "gzip, deflate, br"
  ]
  "connection" => array:1 [
    0 => "keep-alive"
  ]
  "content-length" => array:1 [
    0 => "0"
  ]
]

Let’s add a new header to the request and get it. To do that add route in api.php from the routes directory.

Route::post('/user-info', [UserController::class, 'userInfo']);

Then run the below command that serves Laravel applications on the development server.

php artisan serve

After running the above command, open postman software and run the following URL with the post method,

http://127.0.0.1:8000/api/user-info

Add and Get Header Data

To add a new header click on the headers tab and add the header key and its value. In our case, we added two headers userId and userKey as you can see in the screenshot.

Postman Interface

After adding the header we can access it in controller, all we need to do is pass the same key as an argument in the header() function. For example to access the userId header we need to pass the userId as an argument in the header() function.

$request->header('userId');
/* Output: 10 */

Similarly, we can get the userKey header by passing userKey into the header() function.

$request->header('userKey');
/* Output: secret123  */

In case you pass a header key that does not exist then header() function returns null. Following is an example

$request->header(anyRandomKey);
/* Output: null */

Conclusion

To access header from incoming request, you can use header() function from the Request class. The header key needs to pass as an argument into the header() function.

In this article, we learned how to get header data from incoming requests in Laravel. If you want to learn more about it check out our Laravel page.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Laravel,