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 interceptor is provided like this in my app.module.ts:
{ provide: HTTP_INTERCEPTORS, useClass: MyHttpInterceptor, multi: true }
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
How can I reveal these headers in Angular ?
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);
});
