How to intercept the response in angular 4 with HttpInterceptor
next.handle(req)
returns an observable so you can subscribe to it:
return next.handle(req).map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response and headers you want
event.headers
event.body
}
return event;
})
To learn more about mechanics behind interceptors read:
- Insider’s guide into interceptors and HttpClient mechanics in Angular
you also need to import library
import 'rxjs/add/operator/map';
then you use as below. you also need to return the event object so that it can be received in your subscribe() function.
return next.handle(req).map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response and headers you want
}
return event;
});