7

If I make any request to http://localhost:8000 or http://127.0.0.1:8000 it hangs on status pending. (Exactly as here https://github.com/guzzle/guzzle/issues/1857)

I was told that it isn't related to guzzle and that I should better ask about it here.

I stumbled upon this problem while following laravel.com/docs/5.4/passport

This is the code that hangs:

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'authorization_code',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'redirect_uri' => 'http://example.com/callback',
        'code' => $request->code,
    ],
]);

I tried making GET and POST request to working API routes (tested with postman) and it still hangs when calling the same routes using guzzle.

So is there a way to make requests to my own API while using php artisan serve?

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Carl Kroeger Ihl
  • 311
  • 3
  • 12

4 Answers4

6

Carl has a great solution to this. If you are looking for a quick fix to test your updates - you can get this done by opening up two command prompts. The first would be running php artisan serve (locally my default port is 8000 and you would be running your site on http://localhost:8000). The second would run php artisan serve --port 8001.

Then you would update your post request to:

$response = $http->post('http://localhost:8001/oauth/token', [
    'form_params' => [
        'grant_type' => 'authorization_code',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'redirect_uri' => 'http://example.com/callback',
        'code' => $request->code,
    ],
]);

This should help during your testing until you are able to everything on server or a local virtual host.

busytraining
  • 723
  • 8
  • 14
4

I ended up solving it by using wamp virtualhost instead of php artisan serve. No idea why it doesn't work with localhost though.

UPDATE: Someone was kind enough to explain why it wouldn't work.

In https://github.com/guzzle/guzzle/issues/1857#issuecomment-506962175

The reason for this is php artisan serve is a single thread application. So when we use guzzle to request from it to itself, it basically just tries to finish guzzle request (as a client) first then come to finish that request (as a server), which is impossible.

More info about this: https://php.net/manual/en/features.commandline.webserver.php

Also this answer:

When making calls to itself the thread blocked waiting for its own reply. The solution is to either seperate the providing application and consuming application into their own instance or to run it on a multi-threaded webserver such as Apache or nginx.

Carl Kroeger Ihl
  • 311
  • 3
  • 12
  • 1
    i had exactly the same issue and solved it exactly the same way if someone could explain this it would be awesome – George Jul 03 '18 at 13:18
4
try this.
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use App\User;

class UserController extends Controller
{

    //use AuthenticatesUsers;
    protected function login(Request $request)
    {

         $request->request->add([
                'grant_type'    => 'password',
                'client_id'     => '3',
                'client_secret' => '6BHCRpB4tpXnQvC1DmpT7CXCSz7ukdw7IeZofiKn',
                'scope' => '*'
            ]);

        // forward the request to the oauth token request endpoint
        $tokenRequest = Request::create('/oauth/token','post');
        return Route::dispatch($tokenRequest);
    }

}
Sanju Kaniyamattam
  • 501
  • 1
  • 4
  • 14
0
/**
 * Login function
*/
public function login(Request $request) { 
    
    /* 
        Sample post object
        {
            "username": "test@gmail.com",
            "password": "test123"
        }
    */
    if (Auth::attempt(['email' => request('username'), 'password' => request('password')])) { 
       
        return $this->getToken($request);
    } 
    else { 
        return response()->json(['error'=>'Unauthorised'], 401); 
    } 
}

public function getToken(Request $request) {
    //Get client ID and client Secret
    $client = DB::table('oauth_clients')->where('password_client',1)->first();
    $request->request->add([
        "grant_type" => "password",
        "username" => $request->username,
        "password" => $request->password,
        "client_id"     => $client->id,
        "client_secret" => $client->secret,
    ]);
    // Post to "/oauth/token
    $tokenRequest = $request->create('/oauth/token','post');
    $instance = Route::dispatch($tokenRequest);
    //return token_type, expires_in, access_token, refresh_token
    return response()->json(json_decode($instance->getContent()));

}
Simeon Nortey
  • 131
  • 1
  • 5