Oleksii Klipilin
Oleksii Klipilin

Reputation: 1936

HttpInterceptor for TypeScript client by AutoRest

I'm quite new to TypeScript and Angular. I have http interceptor:

export class HttpTokenInterceptor implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const headersConfig = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    };

    headersConfig['TEST'] = `TEST`;

    const request = req.clone({ setHeaders: headersConfig });
    return next.handle(request);
  }
}

And when I send GET request via HttpClient from '@angular/common/http' let resp = await this.http.get('http://localhost').toPromise(); I can see in headers my TEST header item.

Also I use autorest for generating TypeScript client for my rest services (@microsoft.azure/autorest-core: 2.0.4302) and when I call any method of generated client custom TEST header is not added to the query.
I thought that AutoRest generates also HTTP queries via the same HttpClient which should have been handled by HttpInterceptor...
Maybe I'm doing this completely wrong, should it be working at all in this way? Or is there a way to intercept http requests made by AutoRest generated code?

Upvotes: 0

Views: 472

Answers (0)

Related Questions