【问题标题】:cakephp 3.8.13 handle unauthorized request, response in JSONcakephp 3.8.13 处理未经授权的请求,以 JSON 格式响应
【发布时间】:2021-01-22 10:14:51
【问题描述】:

我在我的 CakePHP 应用程序中实现了https://github.com/ADmad/cakephp-jwt-auth。我的问题是每当有未经授权的请求时,它都会以 HTML 响应。

    <html>
        <head>
            <meta charset="utf-8"/>
            <title>
            CakePHP: the rapid development php framework:
            Error   </title>
            <link href="/favicon.ico" type="image/x-icon" rel="icon"/>
            <link href="/favicon.ico" type="image/x-icon" rel="shortcut icon"/>
            <link rel="stylesheet" href="/css/cake.generic.css"/>
        </head>
        <body>
            <div id="container">
                <div id="header">
                    <h1>
                        <a href="https://cakephp.org">CakePHP: the rapid development php framework</a>
                    </h1>
                </div>
                <div id="content">
                    <h2>Unauthorized</h2>
                    <p class="error">
                        <strong>Error: </strong>
                        An Internal Error Has Occurred.
                    </p>
                </div>
              </div>
          </body>
    </html>

但根据本教程https://www.bravo-kernel.com/2015/04/how-to-add-jwt-authentication-to-a-cakephp-3-rest-api/,它将以 JSON 响应。本教程的示例响应是

{
    "success": false,
    "data": {
        "message": "You are not authorized to access that location.",
        "url": "\/api\/cocktails.json",
        "code": 401
    }
}

这是我的路由器

Router::prefix('api', function ($routes) {
    $routes->extensions(['json', 'xml']);
    Router::connect('/api/check', ['_method' => 'GET','controller' => 'EmailNotification', 'action' => 'check', 'prefix' => 'api']);
    $routes->fallbacks('DashedRoute');
});

我该如何处理?我想要 JSON 中的未经授权的响应。谢谢。

【问题讨论】:

  • 你也可以发布方法 check() 吗?
  • 我正在使用邮递员。发送请求没有问题。

标签: cakephp error-handling unauthorized cakephp-3.8


【解决方案1】:

您可以尝试使用 Accept:application/json 标头发送您的 http 请求

【讨论】:

    猜你喜欢
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 2019-05-22
    • 2020-04-16
    • 2020-07-23
    • 2014-12-01
    相关资源
    最近更新 更多