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

angular - HttpContextToken always false while using HttpInterceptorFn - Stack Overflow

programmeradmin2浏览0评论

I am implementing a loading spinner that runs for all my http requests via an HttpInterceptorFn using this blog post as a guide: /

loading.interceptor.ts

export const loadingInterceptor: HttpInterceptorFn = (req, next) => {
  const loadingSpinnerService = inject(LoadingSpinnerService);
  console.log('SkipLoading: ', req.context.get(SkipLoading));
  if (req.context.get(SkipLoading)) {
    return next(req);
  }
  loadingSpinnerService.setIsLoading(true);
  return next(req).pipe(
    finalize(() => {
      loadingSpinnerService.setIsLoading(false);
    }),
  );
};

HttpContextToken exported from the loading-spinner.service.ts

export const SkipLoading = new HttpContextToken<boolean>(() => false);

refreshToken API call where I wish to skip the spinner

refreshToken() {
    return this.http.post<TokenResponse>(
      this.apiUrl + '/Token/refresh/' + this.siteUserId$.value?.toString(),
      { context: new HttpContext().set(SkipLoading, true) },
    );
  }

Using console.log() I am noticing that my SkipLoading token is always false. Is there some difference in the implementation when using HttpInterceptorFn rather than HttpInterceptor class?

EDIT: After changing the API type to GET the context token evaluates to true as expected. But I must still be doing something wrong as I need this functionality for POST requests as well.

I am implementing a loading spinner that runs for all my http requests via an HttpInterceptorFn using this blog post as a guide: https://blog.angular-university.io/angular-loading-indicator/

loading.interceptor.ts

export const loadingInterceptor: HttpInterceptorFn = (req, next) => {
  const loadingSpinnerService = inject(LoadingSpinnerService);
  console.log('SkipLoading: ', req.context.get(SkipLoading));
  if (req.context.get(SkipLoading)) {
    return next(req);
  }
  loadingSpinnerService.setIsLoading(true);
  return next(req).pipe(
    finalize(() => {
      loadingSpinnerService.setIsLoading(false);
    }),
  );
};

HttpContextToken exported from the loading-spinner.service.ts

export const SkipLoading = new HttpContextToken<boolean>(() => false);

refreshToken API call where I wish to skip the spinner

refreshToken() {
    return this.http.post<TokenResponse>(
      this.apiUrl + '/Token/refresh/' + this.siteUserId$.value?.toString(),
      { context: new HttpContext().set(SkipLoading, true) },
    );
  }

Using console.log() I am noticing that my SkipLoading token is always false. Is there some difference in the implementation when using HttpInterceptorFn rather than HttpInterceptor class?

EDIT: After changing the API type to GET the context token evaluates to true as expected. But I must still be doing something wrong as I need this functionality for POST requests as well.

Share Improve this question edited Nov 21, 2024 at 5:13 Naren Murali 58.9k5 gold badges44 silver badges77 bronze badges asked Nov 21, 2024 at 4:42 Asad KothsAsad Koths 918 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

For post the syntax is given in the HttpClient - Angular.dev as:

post(url: string, body: any, options: { headers?: HttpHeaders | 
{ [header: string]: string | string[]; } | undefined; context?: HttpContext | undefined; 
observe?: "body" | undefined; params?: HttpParams | { ...; } | undefined; 
reportProgress?: boolean | undefined; responseType: "arraybuffer"; withCredentials?: boolean | undefined; 
transferCache?: boolean ...): Observable<ArrayBuffer>;

Notice that the second argument is the body of the post request, so the context should be passed in an object as the third argument.

  refreshToken() {
    return this.http.post<TokenResponse>(
      this.apiUrl + '/Token/refresh/' + this.siteUserId$.value?.toString(),
      null, { context: new HttpContext().set(SkipLoading, true) },
    );
  }
发布评论

评论列表(0)

  1. 暂无评论