最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - switchMap is not executing its code in Http Interceptor - Stack Overflow

programmeradmin1浏览0评论

I am facing an issue with the switchMap in interceptor. But in angular service it works fine. unable to find the reason why switchMap is not executing its code. Need help please.

getAccessToken

    getAccessToken(): Observable<string> {
    const headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
    
    const body = new HttpParams()
        .set('client_id', AppConfig.settings.ClientId)
        .set('client_secret', AppConfig.settings.ClientSecret);
    
    return this.http.post<{ access_token: string }>(AppConfig.settings.tokenURL, body.toString(), { headers }).pipe( map(response => response.access_token) );
    }

Interceptor:

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      
        return this.authService.getAccessToken().pipe(
          switchMap(token => {
            const headers = new HttpHeaders({
              'Authorization': `Bearer ${token}`,
              'Accept': 'application/json'
            });
            const clonedRequest = req.clone({ headers });
            return next.handle(clonedRequest);
          })
        );
      }
发布评论

评论列表(0)

  1. 暂无评论