I am missing http headers in a response from my HttpInterceptor. I can get a body but not headers. Please see attached output and my code.
@Injectable()
export class ApiVersionInterceptor implements HttpInterceptor {
intercept(
req: import("@angular/common/http").HttpRequest<any>,
next: import("@angular/common/http").HttpHandler
): import("rxjs").Observable<import("@angular/common/http").HttpEvent<any>> {
return next.handle(req).pipe(
tap(httpEvent=>{
// Skip request
if(httpEvent.type === 0){
return;
}
console.log("response: ", httpEvent);
})
);
}
}
Wohoo I fixed my problem.
It is not Angular problem but server problem. I need to add another header:
"access-control-expose-headers": "mintargetapiversion"
Angular ignores custom headers if they are not specified in "access-control-expose-header"
Use httpEvent.headers.get()
method like shown below:
@Injectable()
export class ApiVersionInterceptor implements HttpInterceptor {
intercept(
req: import("@angular/common/http").HttpRequest<any>,
next: import("@angular/common/http").HttpHandler
): import("rxjs").Observable<import("@angular/common/http").HttpEvent<any>> {
return next.handle(req).pipe(
tap((httpEvent: HttpEvent<any>) =>{
// Skip request
if(httpEvent.type === 0){
return;
}
console.log("response: ", httpEvent);
let minTargetApiVersion : string;
if (httpEvent instanceof HttpResponse) {
if(httpEvent.headers.has('mintargetapiversion')) {
minTargetApiVersion = httpEvent.headers.get('mintargetapiversion');
}
}
})
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.