httpclient - HttpClient 不返回所有标头
问题描述
我正在尝试从带有 angular 6 应用程序的 httpresponse 中读取一些自定义标头(“etag”):
getProvider(providerName: string, version: string): Observable<any> {
if (providerName && version) {
var url = `${this.baseUrl}/v1/config/provider/version/${version}/${providerName}`;
return this._http.get(url, { observe: 'response' })
.pipe(
map(response => {
console.log("keys: " + response.headers.keys());
return response.body;
}),
tap(provider => this.log(`getProvider `)),
catchError(this.handleError('Provider error', []))
);
}
else {
of(null);
}
}
这仅返回 2 个没有“etag”标头的标头:
键:内容类型,缓存控制
我的 asp (mvc) Web api 配置为接受 CORS:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Headers" value= "etag, Accept, Accept-CH, Accept-Charset, Accept-Datetime, Accept-Encoding, Accept-Ext, Accept-Features, Accept-Language, Accept-Params, Accept-Ranges, Access-Control-Allow-Credentials, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin, Access-Control-Expose-Headers, Access-Control-Max-Age, Access-Control-Request-Headers, Access-Control-Request-Method, Age, Allow, Alternates, Authentication-Info, Authorization, C-Ext, C-Man, C-Opt, C-PEP, C-PEP-Info, CONNECT, Cache-Control, Compliance, Connection, Content-Base, Content-Disposition, Content-Encoding, Content-ID, Content-Language, Content-Length, Content-Location, Content-MD5, Content-Range, Content-Script-Type, Content-Security-Policy, Content-Style-Type, Content-Transfer-Encoding, Content-Type, Content-Version, Cookie, Cost, DAV, DELETE, DNT, DPR, Date, Default-Style, Delta-Base, Depth, Derived-From, Destination, Differential-ID, Digest, ETag, Expect, Expires, Ext, From, GET, GetProfile, HEAD, HTTP-date, Host, IM, If, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Keep-Alive, Label, Last-Event-ID, Last-Modified, Link, Location, Lock-Token, MIME-Version, Man, Max-Forwards, Media-Range, Message-ID, Meter, Negotiate, Non-Compliance, OPTION, OPTIONS, OWS, Opt, Optional, Ordering-Type, Origin, Overwrite, P3P, PEP, PICS-Label, POST, PUT, Pep-Info, Permanent, Position, Pragma, ProfileObject, Protocol, Protocol-Query, Protocol-Request, Proxy-Authenticate, Proxy-Authentication-Info, Proxy-Authorization, Proxy-Features, Proxy-Instruction, Public, RWS, Range, Referer, Refresh, Resolution-Hint, Resolver-Location, Retry-After, Safe, Sec-Websocket-Extensions, Sec-Websocket-Key, Sec-Websocket-Origin, Sec-Websocket-Protocol, Sec-Websocket-Version, Security-Scheme, Server, Set-Cookie, Set-Cookie2, SetProfile, SoapAction, Status, Status-URI, Strict-Transport-Security, SubOK, Subst, Surrogate-Capability, Surrogate-Control, TCN, TE, TRACE, Timeout, Title, Trailer, Transfer-Encoding, UA-Color, UA-Media, UA-Pixels, UA-Resolution, UA-Windowpixels, URI, Upgrade, User-Agent, Variant-Vary, Vary, Version, Via, Viewport-Width, WWW-Authenticate, Want-Digest, Warning, Width, X-Content-Duration, X-Content-Security-Policy, X-Content-Type-Options, X-CustomHeader, X-DNSPrefetch-Control, X-Forwarded-For, X-Forwarded-Port, X-Forwarded-Proto, X-Frame-Options, X-Modified, X-OTHER, X-PING, X-PINGOTHER, X-Powered-By, X-Requested-With"/>
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
</customHeaders>
</httpProtocol>
请知道如何获取此标头吗?
解决方案
解决方案是添加 Access-Control-Expose-Headers:
<add name="Access-Control-Expose-Headers" value="etag, Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma" />
推荐阅读
- java - 有没有办法在 Java 中模仿 Ruby 中的类实例变量概念?
- android - ListAdapter:在片段中抓取 item 或 itemPos
- html - 使用 thymeleaf 在邮件中发送图像
- cakephp - 通过 http 客户端调用 api 显示错误 Missing CSRF token cookie
- java - 通过 URL 在 VideoView 中显示视频
- jquery - 为什么无法在包管理器控制台中运行 bower update 命令
- symfony - snc/redis-bundle 在控制器中获取 redist 集群 - symfony 3+
- javascript - Electron - 应用内购买 - 如何使用 PHP 验证苹果收据?
- json - 在 PUG 中渲染 json 数组
- angular - 导入角度材质样式时出错