Umang Mehra
Umang Mehra

Reputation: 57

Remove Authorization from request headers in particular http call

Interceptor -

export class AuthInterceptor implements HttpInterceptor {
  constructor(private loaderService: LoaderService, private router: Router) { }
  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    this.showLoader();
    request = request.clone({
      setHeaders: {
        Authorization: `${localStorage.getItem('authToken')}`, 
      }
    });
    return next.handle(request).pipe(tap((event: HttpEvent<any>) => {
      if (event instanceof HttpResponse) {
        this.hideLoader();
      }
    },
      error => {
        this.hideLoader();
        if (error.status === 401) {
          this.router.navigate(['../auth']);
        }
      }),
    );
  }

Service -

 getList() {
    return this.commonHttpService.get(`common/List`);  
  }

TS API call -

 List() {
    this.Service.getList()
      .subscribe((res: any) => {
        this.Data = res.body.data;
      });
  }

I want to remove the Authorization from request headers when I call this API.

How can we do that? Searched many answers on stackoverflow but nothing worked. Can someone please help?

Upvotes: 0

Views: 314

Answers (1)

StPaulis
StPaulis

Reputation: 2926

On the interceptor you can have a list of Uris that you won't add the Auth header.

if (unauthorizedUris.every(x => x !== request.url))
    request.clone({
        setHeaders: {
            Authorization: `${localStorage.getItem('authToken')}`, 
    }});

Upvotes: 1

Related Questions