The following code does not log custom headers from the server. Am I missing something - the only headers written out are content-type and content-length.
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { LoaderService } from './loader.service';
import { AuthService } from './services/auth-service';
import * as _ from 'lodash';
import { tap } from 'rxjs/internal/operators/tap';
@Injectable()
export class LoaderInterceptor implements HttpInterceptor {
constructor(public loaderService: LoaderService,
private authService: AuthService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.loaderService.show();
if (!_.isNil(this.authService.authenticatedUser) && this.authService.authenticatedUser.token) {
request = request.clone({
setHeaders: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.authService.authenticatedUser.token}`
}
});
}
return next.handle(request).pipe(tap((resp: HttpResponse<any>) => {
if (resp instanceof HttpResponse) {
for (const h of resp.headers.keys()) {
console.log('header', h);
}
}
},
(resp: any) => {
if (resp instanceof HttpErrorResponse) {
if (resp.status !== 401) {
return;
}
this.loaderService.setUnAuthorized();
}
}, () => { this.loaderService.hide(); }));
}
}