skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
    1. IlluminateHttpRequest object
    use IlluminateHttpRequest;
    
    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);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search