2016-09-30 73 views
2

我有一些麻烦让这个中间件在Laravel工作。我正在使用Tymon的JWT来检查一个标记是否有效,但是当我用Postman测试时,我根本没有得到任何响应,而不是我设置的HTTP 4xx错误。邮差显示没有响应Laravel中间件异常

public function handle($request, Closure $next, $guard = null) 
{ 

    try { 

     JWTAuth::parseToken()->authenticate(); 

    } catch (TokenExpiredException $e) {  //Token Expired 
     return response('Token Expired', 440); 
    } catch (TokenInvalidException $e) {  //Token Invalid 
     return response('Token Invalid', 401); 
    } catch (JWTException $e) { 
     echo("TEST"); 
     return response('Token Exception', 499); 
    } catch (TokenBlacklistedException $e) { //Token Blacklisted 
     return response('Token Blacklisted', 403); 
    } 

    return $next($request); 
} 

当我尝试在邮递员调用的标题中使用没有标记时,它告诉我没有来自服务器的响应。

Could not get any response 
There was an error connecting to api.website.dev/test. 
Why this might have happened: 
The server couldn't send a response: 
    Ensure that the backend is working properly 
Self-signed SSL certificates are being blocked: 
    Fix this by turning off 'SSL certificate verification' in Settings > General 
Client certificates are required for this server: 
    Fix this by adding client certificates in Settings > Certificates 
Request timeout: 
    Change request timeout in Settings > General 

的例外似乎被触发,但我只能从服务器获取的反应,当我把东西像一个echo语句如上面的代码所示。 我一直在我的代码的其他部分以及我无法创建排队的作业,并且邮差说服务器没有响应相同的Could not get any response消息的这个问题。

这是Laravel的问题还是我做错了什么? 预先感谢您的帮助!

回答

3

原来这工作得很好。我用爪子而不是邮差,它用正确的消息通过HTTP向我显示了正确的错误代码。看起来问题是邮差。

+0

下次发布您正在使用的邮差版本时会有帮助,以及常规的'curl'响应是什么 - 以防万一它是[this](https://github.com/postmanlabs/postman) -app-support/issues/2214)或[this](https://github.com/postmanlabs/postman-app-support/issues/1041)。因为您也在为您的测试用例发送不寻常的(非标准)HTTP响应代码,这也可能会导致测试工具出现问题,因此最好使用404等标准响应进行测试。 – Leith

+0

这很可能是由于自定义为Paw编写的HTTP lib,可以读取完整的错误代码和状态消息,大多数其他库不提供此信息请参阅https://paw.cloud/docs/advanced/http-libraries –