Empty headers in HTTP Response with Angular 4.3's HttpInterceptor
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
This is how I send my HTTP request:
return this.http.get(url, { observe: 'response' })
I would like to read the HTTP headers of a HttpResponse in my HttpInterceptor:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request)
.do(event => {
if (event instanceof HttpResponse) {
this.logger.logDebug(event); // Headers are missing here
}
})
.catch((err: HttpErrorResponse) => {
// Do stuff
}
}
The event seems to have no headers, and even in the Chrome Dev Console I cannot see any headers:
However, when using Postman, I can see the headers in the response (as expected)
Connection →keep-alive Content-Length →14766 Content-Type →application/json Date →Fri, 04 Aug 2017 14:50:46 GMT Server →WildFly/10 X-Powered-By →Undertow/1
The official docs for HTTP says to get the headers like this:
http
.get<MyJsonData>('/data.json', {observe: 'response'})
.subscribe(resp => {
// Here, resp is of type HttpResponse<MyJsonData>.
// You can inspect its headers:
console.log(resp.headers.get('X-Custom-Header'));
// And access the body directly, which is typed as MyJsonData as requested.
console.log(resp.body.someField);
});
Expected behavior
I was expecting to see the same headers in Angular as with Postman
Environment
Angular version: 4.3.3
Browser:
- [x] Chrome (desktop) version XX
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Intercepting HTTP Response headers with Angular 4.3's ...
I found the answer. It was (of course) a CORS Problem. I am using a CORS Filter and I needed to explicitely expose...
Read more >Intercepting Http Response Headers With Angular 4.3'S ... - ADocLib
In my Angular 7 project I receive an error [ts] Cannot find module Empty import TypeScript ES6 Some modules do not export any...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@robwormald You were right. It was a CORS problem. I needed to explicitely expose my custom header. Thank you
Can you share the code you’re using to actually read the headers? Logging the
HttpHeaders
instance is going to be misleading - headers are lazily parsed from the response header string on first access. It looks like your instance has alazyInit
function and is poised to perform this parsing when the headers are first accessed.