title: Angular 6 HttpInterceptor - when getting 401 refresh the token and create the same request
body: <p>I'm using the <code>HttpInterceptor</code> in Angular 6, and trying to built a refresh token mechanisim:</p>
  
  <p>When httpClient request get's <code>401</code> status code (unauthorised) the <code>HttpInterceptor</code> will create a request that will refresh the token, it will update the headers of the first request and call it again with the new token.</p>
  
  <p>The code is working until the stage that I need to recall the original request again with the new token that got from the refresh token request.
  This is my code:</p>
  
  <pre><code>export class MyInterceptor implements HttpInterceptor {
  
  constructor(public restService:RestService){}
  
  intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
   return next.handle(request).pipe(
      tap(event =&gt; {
        if (event instanceof HttpResponse) {
          console.log('succeed');
        }
      }, error =&gt; {
          if(error.status==401){
              this.restService.refreshToken().subscribe(response =&gt; {
                  this.restService.updateHeaders(response['token']);
                  const newRequest = request.clone();
                  return next.handle(newRequest);
                });
          }
      })
    )
  }
  }
  </code></pre>