cakephp 3.8.13 处理未经授权的请求,在 JSON 中响应
cakephp 3.8.13 handle unauthorized request, response in JSON
我已经在我的 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 中的未经授权的响应。谢谢。
您可以尝试使用 Accept:application/json header
发送您的 http 请求吗
我已经在我的 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 中的未经授权的响应。谢谢。
您可以尝试使用 Accept:application/json header
发送您的 http 请求吗