So im new to guzzle and building API's , I have used Laravel Passport and on one GET call its fine. I have written a POST call and getting a 500 error in return
Post function
public function newsSingle() {
$request = (new GuzzleHttp\Client)->post('http://138.68.180.100/news/article/single', [
'headers' => [
'Authorization' => 'Bearer '.session()->get('token.access_token'),
'post_id' => $_POST['post_id']
]
]);
$news = json_decode((string)$request->getBody());
return view('pages.newsingle', compact('news'));
}
Which does add the post item POST Data post_id "3"
on the other end I have
Route:
Route::post('news/article/single', 'ApiController@singlePost')->middleware('auth:api');
Controller function:
public function singlePost(Request $request) {
$article = Articles::where('id', $request['post_id'])->get();
return $article;
}
my error:
Server error: `POST http://ipaddress/news/article/single` resulted in a `500 Internal Server Error` response: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta name="robots" content="noindex,nofollow (truncated...)
We found the similar issue with Guzzle for External API calls when response code is 500 and got Server error: and exception is thrown. There is a work around to do a bypass mechanism by catching the exception due to BadResponseException to return as response. below is the code for performing this. :)
catch (\GuzzleHttp\Exception\BadResponseException $e) {
return $e->getResponse()->getBody()->getContents();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With