Home > other >  How to get header request value in my laravel app on production?
How to get header request value in my laravel app on production?

Time:04-15

In my laravel app request header value returns an expected value on my localhost. but when i upload project on server its returning null value. I created a middleware Authkey.php

public function handle(Request $request, Closure $next)
{
    $token = $request->header('API_ACCESS_KEY');
    dd($token);
    if ($token !=env('API_ACCESS_KEY')) {
           return response()->json(['message' => 'Unauthorized'],401);
    } 
    return $next($request);
}

CodePudding user response:

Replace the API_ACCESS_KEY in your request with Api-Access-Key , also update it at your middleware too ,

as the underscores are invalid characters for header names,

Translation of headers to environment variables is more strict than
 before to mitigate some possible cross-site-scripting attacks via header injection.
 Header names containing invalid characters (including underscores) are
 no longer converted to environment variables.

for more info please check apache new features

CodePudding user response:

  1. Illuminate\Http\Request object
use Illuminate\Http\Request;

public function handle(Request $request, Closure $next)
{
    $headers = $request->header();
    $user_agent = $request->header('API_ACCESS_KEY');
}
  1. \Request class header() function
public function handle(Request $request, Closure $next)
{
    $headers = \Request::header();
    // or pass parameter to get specific header
    $user_agent = \Request::header('API_ACCESS_KEY');
}
  1. apache_request_headers() method
public function handle(Request $request, Closure $next)
{
    $headers = apache_request_headers();

    dd($headers);
}
  1. Global getallheaders() method
public function handle(Request $request, Closure $next)
{
    $headers = getallheaders();

    dd($headers);
}
  • Related