How to get custom header from HTTP response in Laravel 5?

16,733

Solution 1

Are you talking about get parameter or something? If so, use:

request()->accessing_from;

For header you should use:

request()->header('accessing_from');

The working solution for this was the answer (the last one) of daver here: Laravel get request header

Solution 2

Have you tried simple php?

<?php
// Replace XXXXXX_XXXX with the name of the header you need in UPPERCASE
$headerStringValue = $_SERVER['HTTP_XXXXXX_XXXX'];

Full answer: https://stackoverflow.com/a/541463/3548658

The docs says: https://laravel.com/api/5.3/Illuminate/Http/Request.html#method_header

use Request;
Request::header('accessing_from');
Share:
16,733
Caius
Author by

Caius

How you do anything, is how you do everything.

Updated on June 11, 2022

Comments

  • Caius
    Caius almost 2 years

    I'm trying to access a custom header from the Request in Laravel. The header name is "accessing_from". Listing all the headers in Laravel, gives me only the "standard ones", but the one that I've set isn't present in the list. Checking in the browser network tab I can see that the header gets sent. So I'm wondering how to access it from within Laravel.

    I'm using Angular2 to make the request with the default http service.

    The Laravel's $response->header() dump:

    enter image description here

    The web inspector's log:

    enter image description here Thanks to anyone!

  • Caius
    Caius over 7 years
    no, I'm willing to get the exact header from the request, it's not a parameter
  • Alexey Mezenin
    Alexey Mezenin over 7 years
    If request()->header('accessing_from'); doesn't show anything, you may want to read daver answer here.
  • Caius
    Caius over 7 years
    Trying to access the header with request()->header('accessing_from') returns a empty array :(
  • Caius
    Caius over 7 years
    @HRLET, already tried that before, unfortunately none of both methods works.
  • Artistan
    Artistan over 4 years
    Need to use Accessing-From - updated answer stackoverflow.com/a/59427256/372215