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:
- http://example.dev/api/test (returns 'hello') )
- 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.