1

Well i have a strange issue, I have 2 instances of laravel5. One works as an api, while the other one works as a frontend website. I am using guzzle 6 (an http client library) on the frontend website for api requests.

I have 2 routes on the api:

  1. http://example.dev/api/test (returns 'hello') )
  2. http://example.dev/api/authenticate ( returns OAUTH2 or JWT access_token)

The first route (test) works fine with both postman and GUZZLE and Here is the code. (Note: The form params are not necessary for this route, but i have included to show the difference )

    $client = new Client(['base_uri' => 'http://example.dev/api/']);

    $response = $client->request('POST', 'test', [
        'form_params' => [                    // Not necessary
            'username' => 'email@example.com',// Not necessary
            'password' => 'password',         // Not necessary
        ]
    ]);

    $result = json_decode($response->getBody()->getContents(), true); 
    dd($result);

The Second Route (authenticate) Works with POSTMAN and we get a token back but not with guzzle.

$client = new Client(['base_uri' => 'http://example.dev/api/']);

$response = $client->request('POST', 'authenticate', [
    'form_params' => [                    
        'username' => 'email@example.com',
        'password' => 'password',         
    ]
]);

$result = json_decode($response->getBody()->getContents(), true); 
dd($result);

The error i recieve on the second route is "ServerException in Middleware.php line 68:". Although no middleware has been assigned to it, Another question also has a very similar problem and they solved it with some modification to the above code.

I have been banging my head for 2 days now, and there are so many different versions of guzzle, and every version has its own code. I have tried using both OAuth2 & JWT but i think there is some kind of header missing or something.

It would be really great if you can share the code that works no matter what version of guzzle it is, as i can downgrade it.

Omer Farooq
  • 3,754
  • 6
  • 31
  • 60
  • In my current Guzzle6 line 68 of Middleware.php is `ServerException("Server error: $code", $request, $response)`, which should itself be giving an error. Do you know what the actual server error is, whats the response here? – DavidT Sep 16 '15 at 11:57
  • Well i am sure the error is not from the api. The error is from the used above code. I have removed all the middlewares but its still there. I forgot to add the reference to the other similar questions having the same error with guzzle http://stackoverflow.com/questions/30860235/guzzle-ver-6-post-method-is-not-woking – Omer Farooq Sep 16 '15 at 12:01
  • I have also used guzzle 5 but it also has similar errors. Can you post the code that you use to send requests through post? preferably the part where you request the access token. – Omer Farooq Sep 16 '15 at 12:04
  • Are you able to open a chat? This may be too long for comments – DavidT Sep 16 '15 at 12:06
  • ya sure, that would be really helpfull :) – Omer Farooq Sep 16 '15 at 12:10
  • Here is the link to the chat room http://chat.stackoverflow.com/rooms/89805/laravel-rest-guzzle – Omer Farooq Sep 16 '15 at 12:15

0 Answers0