Laravel Call Internal Api from Controller Using Post and Passing Basic Auth Authorization to Request

I am calling internal API route from controller but unable set headers using laravel 8

$request = Request::create('/api/login/', 'POST',['id' => '1']);
$request->headers->set('Authorization','Basic YWJjOjEyMw==');//not working

$response = Route::dispatch($request);

print_r($response);
3

1 Answer

Have you tried using the Http facade for that?

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders(['Authorization' => 'Basic ...'])->post('/api/login');

$json = $response->json();
$specific_field = $response->json('specific_field');
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest