title: Angular HTTP interceptor for Authentication header
body: <p>I have to put a token inside the 'Authorization' header for every HTTP request.
  So I have developed and registered an HttpInterceptor :</p>
  
  <pre><code>@Injectable()
  export class TokenInterceptor implements HttpInterceptor {
  
    constructor(public authService: AuthService) {
    }  
  
    intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
  
      let modifiedReq;
      const token = this.authService.getToken();
  
      // we need the heck clone because the HttpRequest is immutable
      // https://angular.io/guide/http#immutability
      if (token) {
        modifiedReq = request.clone();
        modifiedReq.headers.set('Authorization', `Bearer ${token}`);
      }
  
      return next.handle(modifiedReq ? modifiedReq : request).pipe(tap(() =&gt; {
          // do nothing
      },
      (err: any) =&gt; {
      if (err instanceof HttpErrorResponse) {
        if (err.status === 0) {
          alert('what the heck, 0 HTTP code?');
        }
        if (err.status !== 401) {
          return;
        }
        this.authService.goToLogin();
      }
    }));
   }
  }
  </code></pre>
  
  <p>But the header seems never to be put on the request sent. What am I doing wrong?</p>
  
  <p>Also, sometimes an errorcode '0' gets caught by the interceptor; what does it mean?</p>
  
  <p>Angular 8.2.11</p>
  
  <p><strong>EDIT 1: ------------------------</strong></p>
  
  <p>I've also tried like this: </p>
  
  <pre><code>request = request.clone({
              setHeaders: {
                  authorization: `Bearer ${token}`
              }
          }); 
  </code></pre>
  
  <p>but still no header has been set. 
  Also, the module is correctly registered in app.module</p>
  
  <pre><code> providers: [{
     provide: HTTP_INTERCEPTORS,
     useClass: TokenInterceptor ,
     multi: true,
   }..
  </code></pre>
  
  <p><strong>EDIT 2 : ------------------------</strong></p>
  
  <p><a href="https://i.stack.imgur.com/pJtRK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pJtRK.png" alt="debugger "></a></p>
  
  <p>Check this image... I'm going crazy.</p>