Local http request with authorization

Viewed 199

I'm trying to query the passport /oauth/clients endpoint from my backend php and I am using this code

$response = Http::get(config('app.url') .'/oauth/clients');

where my app.url is set to http://homestead.test (for development purposes). If I navigate to that endpoint in my browser I get an array of clients as expected but as soon as I try to do this query from my backend and then dump the body like this dd($response->body()); it just returns my website login page.

I'm assuming this is due to the session not being passed correctly as a header but if I dump $response->headers() I get a Set-Cookie header with my app session there.

I am currently using this code to get the clients array and it works, but I think it isn't the best way to get the array as it messes up the Route

$clientRequest = Request::create(config('app.url') .'/oauth/clients');
$response = Route::dispatch($clientRequest);

$clients = json_decode($response->getContent(), true);
1 Answers

Did you see Laravel 5: Calling routes internally?

  1. You can try to instantiate your controller and call the method behind the route.
  2. You can use app()->handle($req) like erwan wrote.

But if that fails, using Route::dispatch seems ok.

Related