0

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?

1 Answers1

0
$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

Moinul Robin
  • 36
  • 1
  • 10
  • 1
    Thanks @moinul-robin brother for your references. your references and [this link](https://laravel.com/docs/8.x/http-client) helped me to solve the issue. – Arafat Rahman Oct 23 '21 at 16:27