4

I'm Bulid an API Authentication System. Everything I can check using Postman But I want to attach this mark portion (please see the image) that's means the header portion send from my controller or middleware with request header.

How Can I do this Stuff. Please see my code sample.

Picture. enter image description here

I try this using a middleware.

 public function handle($request, Closure $next)
{
    $token='Bearer '.$request->bearerToken();
    $response=$next($request);
    $response->header('Authorization',$token);

    return $response;
}

Registered middleware in Kernal.

protected $middlewareGroups = [

    'Header'  =>[
        \App\Http\Middleware\HeaderMiddleware::class
    ],
];

and my routes\api.php

Route::group(['middleware' => ['auth:api','Header']], function(){
Route::post('details', 'API\PassportController@details');
Route::get('test','API\PassportController@test');
});

When I use Middleware it's show this result in Postman. enter image description here

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Gabrielle-M
  • 1,037
  • 4
  • 17
  • 39

2 Answers2

3

It's possible to add some custom headers in middleware. To add custom header to response you need to do it like this:

$response = $next($request);
$response->headers->set('Authorization', 'Bearer '.$request->bearerToken());
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
2

You can do something like below:

 $response = $client->request('POST', '/api/details', [
    'headers' => [
        'Authorization' => 'Bearer '.$token,
        'Accept' => 'application/json',
    ],
]);
Plabon Dutta
  • 6,819
  • 3
  • 29
  • 33