Guzzle返回500错误

时间:2017-12-19 10:32:23

标签: php laravel guzzle laravel-passport

所以我刚刚开始并建立API,我使用了Laravel Passport并在一个GET上调用它的罚款。我写了一个POST调用并返回500错误

发布功能

  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'));
}

哪个添加帖子项目 发布数据 POST_ID " 3"

另一端我有

路线:

Route::post('news/article/single', 'ApiController@singlePost')->middleware('auth:api');

控制器功能:

public function singlePost(Request $request) {
        $article = Articles::where('id', $request['post_id'])->get();
        return $article;
    } 

我的错误:

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...)

1 个答案:

答案 0 :(得分:0)

当响应代码为500并得到Server error:并引发异常时,我们发现Guzzle对于外部API调用也存在类似问题。可以通过捕获BadResponseException引起的异常作为响应来绕过机制。下面是执行此操作的代码。 :)

catch (\GuzzleHttp\Exception\BadResponseException $e) {
    return $e->getResponse()->getBody()->getContents();
}