Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Get Headers Data from Request in Laravel

How to Get Headers Data from Request in Laravel ?

Aman Jain, June 9, 2022November 10, 2023

In this article we will learn to get headers data from request in laravel. Http Request headers are used to send the information to server like what type of content clients accept, Content type or authorizations technique used to validate the request.To retrieve the headers data in laravel we need to call header method of request class.

Sometimes we send some hidden information like token or cookies in the headers which we want to access at server side so to access that in laravel we use header method of request as follow

$request->header(); // get all headers

or

$data = request()->header('header_name'); // array list of headers
$data['field_name'];

Let’s understand it with example step by step

Step 1: Create a fresh laravel project

Open a terminal window and type below command to create a new project

composer create-project --prefer-dist laravel/laravel blog

You can also read this to start with new project

Step 2: Create Route

Next, create a route a and define a inline function in the routes as follow

<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;


Route::post("/send-header-request",function(Request $request ){
     $data = $request ->header();
      dump($request->header('Accept'));
      dd($data);
});

Step 3 : Create Javascript Fetch API Call

To test and send a request in json post format we need to call it from either from client side or server side so we are using here JavaScript fetch method

<script>
fetch('http://localhost:8000/send-json-request', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({test: 1, b: 'Textual content'})
  }).then(data=>{

  });
</script>

ScreenShot:

get header from request in laravel
get header from request in laravel

Related

Php Laravel Laravel 9 headerlaravelrequest

Post navigation

Previous post
Next post

Related Posts

Php How to Share Page to Facebook in Laravel PHP

How to Share Page to Facebook in Laravel PHP ?

March 16, 2022March 16, 2022

Sharing page to facebook from a website using a link is simple as possible. In this article we will not use any package or third party tool to share the url or message to Facebook. Facebook itself provide to share URL using the SDK or a simple url share ,…

Read More
Javascript Laravel Customized Autocomplete JQuery UI

Laravel Customized Autocomplete JQuery UI

July 12, 2022July 12, 2022

Laravel Customized Autocomplete JQuery UI is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel to provide the data in json response. In this tutorial we…

Read More
Php Laravel where not null query with example

How to use laravel where not null and where null query with example ?

January 18, 2022January 18, 2022

In the initial stage of laravel learning you will find many challenges to build the query like where not null and where null so in this article I will show you to create where not null and where null query using laravel eloquent. To create where not null in laravel…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version