简体   繁体   English

狂饮返回500错误

[英]Guzzle returns 500 error

So im new to guzzle and building API's , I have used Laravel Passport and on one GET call its fine. 因此,对于刚开始构建API而言,我是新手,我使用过Laravel Passport,并且在一个GET调用中就很好了。 I have written a POST call and getting a 500 error in return 我写了一个POST呼叫,但返回500错误

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" 哪个确实添加了发布项目POST数据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. 当响应代码为500并出现Server error:时,我们发现Guzzle进行了外部API调用的类似问题Server error:并引发了异常。 There is a work around to do a bypass mechanism by catching the exception due to BadResponseException to return as response. 可以通过捕获BadResponseException导致的异常以作为响应返回来绕过机制。 below is the code for performing this. 下面是执行此操作的代码。 :) :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM