Slim Framework 3 401响应无法设置WWW-Authenticate标头

时间:2017-12-17 22:23:47

标签: rest slim slim-3

使用Slim Framework v3创建REST应用程序。一切都按预期工作,但我似乎无法为响应设置标题。

例如:

$app->any('/[{path:.*}]', function(Request $request, Response $response, $args = null ) use ( $objError, $objDBCon, $objUtil ) {
...
return $response->withAddedHeader( 'WWW-Authenticate', 'API-key realm="restricted"' )
                ->withJson($apiResults, $httpcode);
});

在获取数据,获取正确的http状态代码等方面按预期工作。

例如,我得到了正确的响应JSON

{ "message": "You must be logged in to access this resource" }

我得到了预期的状态代码:

Request Method:GET
Status Code:401 Unauthorized

以及所有标准,正确的标题,内容类型等等。

但似乎无法设置任何额外的标头。

参考文件https://www.slimframework.com/docs/objects/response.html

1 个答案:

答案 0 :(得分:2)

我的名声很低,无法添加评论:

根据手册

  

withAddedHeader方法将新值附加到已存在的相同标题名称的值集

在追加之前你的标题是否已经存在?

我通常会为每个响应创建一个新标题,如下所示:

return $response = $next($request, $response)
            ->withHeader('Access-Control-Allow-Origin', $this->allowedhosts)
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
            ->withStatus(200);

希望这会有所帮助。

相关问题