Home > Software design >  How Can I get responses from api request using brearer token in laravel?
How Can I get responses from api request using brearer token in laravel?

Time:10-24

Problem description:

  • I have a berer api. I just need to pass requests using a controller that actually makes requests and receives the responses. ( NB: without using Postman )

My API root is like this : http://localhost/cloud/website/api/v1/

In api.php

        Route::group(['prefix' => 'v1'], function() {
            Route::group(['middleware' => 'auth:sanctum'], function() {
                 Route::post('entries/sync-info', [EntrySyncInfoController::class, 'index']);
    }
 }

I am trying to get a response from API but keep getting 500 SERVER ERROR.

    $token  ='24|cbG3w1ONkxxeUBhLCGwFjOSLk......';
    
    $response = $client->request('GET', 'http://localhost/cloud/website/api/v1/entries?perPage=50', [
    'headers' => [
        'Authorization' => 'Bearer '.$token,
        'Accept' => 'application/json',
    ],
]);

return $response->body();

But when I request on Postman then it gives me a response perfectly.

postman request's image is here

Also when I hit this url directly on browser it says same error.

I also follow this issue. it returns null.

How can I solve this issue? How can I get responses using bearer token?

CodePudding user response:

$response = $client->get('http://localhost/cloud/website/api/v1/entries?perPage=50', [
    'headers' => [
        'Authorization' => 'Bearer '.$token,
        'Accept' => 'application/json',
    ],
]);

Ref: see this post, also take a look at this

  • Related