如何使用Json格式的Guzzle在Laravel Passport中返回错误

时间:2017-04-15 06:59:20

标签: laravel laravel-passport

当密码授予成功时,它会在json中返回格式,但是当错误在Laravel中返回html格式异常时 enter image description here

1 个答案:

答案 0 :(得分:2)

我自己回答,将配置添加到guzzle 'http_errors' => false会将错误返回给json。

  public function loginapi(Request $request)
{
    $username = $request->username;
    $password =  $request->password;
   $http = new Client;

    $response = $http->post('http://restapi.dev/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => 3,
            'client_secret' => 'Lh66IODOP4pZHF676xZA8ghQiIt9OepqYHVzFEIN',
            'username' => $username,
            'password' => $password,
            'scope' => '',
        ],
        'http_errors' => false //add this to return errors in json
    ]);
        return json_decode((string) $response->getBody(), true);
}