【问题标题】:Slim Framework 3 401 response can't set WWW-Authenticate headerSlim Framework 3 401 响应无法设置 WWW-Authenticate 标头
【发布时间】:2017-12-17 22:23:47
【问题描述】:

拥有一个使用 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

【问题讨论】:

  • 尝试在调用withJson后添加标题,例如withJson()->withAddedHeader(),看看是否有什么不同。

标签: rest slim slim-3


【解决方案1】:

我的声誉太低,无法添加评论:

根据说明书

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);

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 2010-12-17
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2014-10-05
    相关资源
    最近更新 更多