Value not coming to switchMap operator

Discussão em 'Angular' iniciado por andrey.shedko, Maio 9, 2025.

  1. I have quite trivial Interceptor that must do the following:

    • Catch HttpResponse.
    • Find there corresponding headers.
    • Make request to the CORS service.
    • Apply JWT token to headers and proceed with initial request.

    Altough while call to CORS service itself works as expected, when it combined to switchMap (to proceed with initial request) it never receive value from HttpClient.

    1. Interceptor.

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
    catchError((err: HttpErrorResponse) => {
    if (err.headers.get('x-token-url')) {
    const tokenUrl = service.getTokenUrl(err)
    return this.service.getToken(tokenUrl).pipe(
    switchMap((newToken) => {
    const clonedReq = service.applyToken(req, newToken)
    return next.handle(clonedReq);
    })
    );
    }
    return throwError(err);
    })
    );


    2.getToken method

    public getToken(tokenUrl: string): Observable<JwtToken> {

    return this.httpClient.get<JwtToken>(tokenUrl, {
    observe: "body",
    responseType: "json",
    withCredentials: true,
    });
    }

    Continue reading...

Compartilhe esta Página