4

i'm creating an authentication api using passport from the official docs but i'm stuck on sending GuzzelHttp request

i'v done exactly like the docs but when i want to test with postman no result returned it just stay loading without end

this is my code my controller

$user = new User();
        $user->email = $request->email;
        $user->name = $request->name;
        $user->password = bcrypt($request->password);
        $user->save();

        $http = new Client;

        $response = $http->post('http://localhost:8000/oauth/token', [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => 2,
                'client_secret' => 'x2ESrkADoQEaQ91iMW9kKiIvjKo0LL4RxlUtqtmy',
                'password' => $request->password
            ],
        ]);
        dd($response);

        return response([
            'success'=> true,
            'successMessageKey' => 'userCreatedSuccessfully' ,
            'data'=>json_decode((string) $response->getBody(), true)
        ]);

and my route

Route::post('users/register',[
'uses' => 'Api\AuthController@register'
]);

and when i run my route i got no result like this and stuck in loading

error

Amor.o
  • 491
  • 8
  • 21

4 Answers4

1

The issue is when using php artisan serve, it uses a PHP server which is single-threaded.

The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

You can do this solution:

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.

Or 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' => 'password',
            'client_id' => 2,
            'client_secret' => 'x2ESrkADoQEaQ91iMW9kKiIvjKo0LL4RxlUtqtmy',
            'password' => $request->password
        ],
    ]);

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

busytraining
  • 723
  • 8
  • 14
0

Try including password_confirmation in your parameters in Postman. That's what I did in my laravel passport project

enrique
  • 134
  • 10
  • the problem is not in parameters because i tried the same request in guzzle by postman and it works fine , the problem here is with guzzle – Amor.o Mar 23 '19 at 16:17
  • Ahhh okay. Try: instead of $http = new Client;... put this $http = new GuzzleHttp\Client – enrique Mar 24 '19 at 09:37
-1

I findout the problem in your code

please check the parameter which you sent to auth/token. username is missing in your request

  • yes i know i removed it just for test, because i want to see just a response, so i tried to removed it to see a ( parameterMissing ) response but also i didn't see anything – Amor.o Mar 23 '19 at 12:43
-1

You need pass array of form_params as json

e.g

$response = $http->post('http://localhost:8000/oauth/token', [
            'form_params' => json_encode([
                'grant_type' => 'password',
                'client_id' => 2,
                'client_secret' => 'x2ESrkADoQEaQ91iMW9kKiIvjKo0LL4RxlUtqtmy',
                'password' => $request->password
            ]),
 ]);
Vishal Ribdiya
  • 840
  • 6
  • 18
  • 1
    i think it must be an Array, i tried ur example and it show me this err "message": "http_build_query(): Parameter 1 expected to be Array or Object. Incorrect value given" – Amor.o Mar 23 '19 at 13:09
  • also pass `headers => ['Accept' => 'application/json']` – Vishal Ribdiya Mar 25 '19 at 05:09