Search code examples
laravelapache

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


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);
}

Solution

  • 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