Angular 2 rc.6
撰写的 typescript 2
我有一个试图设置自定义标头的Http
服务的包装器。在下面的代码中,options
是我向RequestOptions
提供的Http.get()
对象:
//if the content type is not set, use application/json
if(!options.headers.has("Content-Type")){
options.headers.append("Content-Type", "application/json")
}
//set the X-GFX-REQUESTER header
if(!options.headers.has("X-CUSTOM-HEADER")){
options.headers.append("X-CUSTOM-HEADER", "value");
}
当我的应用程序发出请求时,浏览器的Firefox 48
)网络日志会显示:
Content-Type:" application / json"
x-custom-header:" value"
知道为什么第二个标题的标题名称会被置为小写吗?
PS:谢谢你的回答。真的,但是;这种不一致并不会打扰别人吗?如果Angular
想要小写我设置的标题,为什么不小写所有标题???
答案 0 :(得分:1)
答案 1 :(得分:1)
据我记得,最近修复了这个问题,以便使用全小写标题,以减少错误。根据规格标题不应该区分大小写,因此它无论如何都不重要。
答案 2 :(得分:0)
正如@GuntherZochbaer和@igorzg指出的那样,规范要求HTTP头名称不区分大小写。因此,如果我的应用程序依赖于特定的大小写,我应该更改我的应用程序,而不是试图解决Angular
的限制
我的基本问题是我的服务器预期标头具有与之前设置的相同的外壳。见this github discussion。
我的解决方案(服务器端)是编写一个自定义的getter,它将返回所请求的标头,而不管套管。我没有直接访问标题,而是称之为getter。
<强> PHP 强>
/**
* Provided a request header name, returns the value (case-insensitive fetch).
* Returns NULL if the header is not found
*/
function fetchHeader($headerName){
//http request headers: an associative array
$headers = apache_request_headers();
if($headers===false) return null;
//lowercase all array keys
$headers = array_change_key_case($headers,CASE_LOWER);
$headerName = strtolower($headerName);
return isset($headers[$headerName])? $headers[$headerName] : null;
}