Search code examples
laravel-5requesthttp-headerslumen

Get authorization from Lumen and header from Laravel


In project, all API calls are from Lumen. Front is developed in Laravel. We are calling Lumen APIs using Guzzle http client guzzleHttp.

Now, I need to set header Authorization for all API calls so I'm sending token in header from Laravel but in Lumen I can't get token in Lumen request header.

Below is the example code.

Laravel controller code:

public function get_category(){


    $accessToken = 'kjdhfdkjfhdkjfhfjkdf9875443213456';


    $response = \Guzzle::request("GET","example.com", "categories",['headers' => [
        'Authorization' => $accessToken
    ]]);

    $category_all = json_decode($response->getBody()->getContents(),true);

    return $category_all;

}

Lumen middleware code:

 public function handle($request, Closure $next)
{

   dd($request);

}

In Lumen request I can't get token in request header.


Solution

  • If you're using Apache, by default it remove the Authorization header. You've to add this settings in the .htaccess project's file or in the apache .conf file (usually in /etc/apache2/sites-available/):

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
    
    Header set Access-Control-Allow-Headers "Authorization"