Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Header Authorization key in laravel controller?

Trying to get the header authorization key in controller for making an API. Request is making from fiddler.

$headers = apache_request_headers(); 

And the $header contains an array.

Array (     [User-Agent] => Fiddler     [Host] => localhost:8000     [Content-Length] => 102     [Authorization] => TestKey ) 

If am trying like this to fetch the Authorization , its throwing error.

$header['Authorization] 

Error :

Undefined index: Authorization 

Tried many ways to get the authorization, but not working anything. Is there any way to fetch this?

like image 284
Sunil Avatar asked May 03 '16 10:05

Sunil


People also ask

How can I get bearer token in Laravel?

A- Get Laravel Bearer token: * Get the bearer token from the request headers. So you should just invoke this method to get the bearer token: $token = $request->bearerToken(); And then get what you expect.

How do I pass the authorization header in GET request?

To send a GET request with a Bearer Token authorization header, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.


1 Answers

To get headers from the request you should use the Request class

public function yourControllerFunction(\Illuminate\Http\Request $request) {     $header = $request->header('Authorization');      // do some stuff } 

See https://laravel.com/api/5.5/Illuminate/Http/Request.html#method_header

like image 166
Szenis Avatar answered Oct 08 '22 10:10

Szenis