Angular 6 Interceptor not working as expected

Atul

I have written an Http Interceptor in Angular 6 . The goal is to reload the home page when 302 status is returned. (any back end API returning 302 status ) I am currently not able to understand why the Interceptor is behaving the way it is.

Below is the code :

@Injectable()
export class ResponseHandler implements HttpInterceptor{


    constructor(){

    }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


     return next.handle(request).pipe(


            map((event: HttpEvent<any>) => {

                if(event instanceof HttpResponse){
                    if(event.status === 302){
                        console.log('HttpResponse 302 status ');
                        window.location.reload();
                        return EMPTY;
                    }
                }

            })
        )

    }

}

Actual Behaviour :

1)I am first accessing the home page explicitly. (e.g: /home)

2)But the strange thing is , the initial call to fetch home age itself is not happening

3)It seems request is blocked reaching the server.

4)When interceptor is removed , this initial call is happening (/home)

I am not able to understand why is this behaviour ?

Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .

It should be intercepted only when HttpResponse is returned with 302 error code .

Can anybody please help here ?

Dmitriy Kavraiskyi

The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.

         map((event: HttpEvent<any>) => {

            if(event instanceof HttpResponse){
                if(event.status === 302){
                    console.log('HttpResponse 302 status ');
                    window.location.reload();
                    return EMPTY;
                }
            }

            return event;  // here you return value back
        })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Authentication Interceptor not working in angular 6

Http Interceptor not working in angular 6

Angular 6 - Modifying response body in interceptor not working

Min and max directives not working as expected in Angular 6

Angular 6: refresh token not working as expected

My ngIf Else is not working as expected in Angular 6

Ionic 4 - Angular 6 formArrayName not working as expected

Angular 4.3 Interceptor not working

Angular Logout Interceptor Not Working Properly

Ionic Angular Auth Interceptor not working

Angular 7 HTTP Interceptor is not working

Angular 6 - Interceptor Preflight (CORS)

Angular 6 - ng-dirty and ng-invalid not working as expected

HTML5 Drag and drop not working as expected on Angular 6

Angular 6: Observable async binding not working as expected after HttpErrorResponse

Bootstrap 4 pills is not working as expected in Angular6

Angular6: RxJS switchMap operator is not working as expected

angular $resource not working as expected

Angular BehavioralSubject not working as expected

angular filter not working as expected

Angular ngSwitchCase not working as expected

Http interceptor on response not working in angular 5

How to setup interceptor header in Angular 6

Angular 6 HTTP Interceptor not setting headers

Interceptor not intercepting http requests (Angular 6)

Interceptor not intercepting http POST requests (Angular 6)

Angular 6 - Auth token interceptor not adding headers

ExpressionChangedAfterItHasBeenCheckedError error because of loader interceptor in angular 6

Angular 2 routers not working as expected