I have set a HttpInterceptor for setting an api key in the request headers:
export const authInterceptor: HttpInterceptorFn = (req, next) => {
console.log('authInterceptor');
console.log('request', req.method, req.url);
req = req.clone({
setHeaders: {
ApiToken: '*****************************',
},
});
return next(req);
};
And included it in the app.config:
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(withInterceptors([authInterceptor])),
{
provide: ApiConfiguration,
useValue: { rootUrl: 'http://localhost:5042' }
},
provideServiceWorker('ngsw-worker.js', {
enabled: !isDevMode(),
registrationStrategy: 'registerWhenStable:30000',
}),
],
};
And then bootstrap the application in main.ts. All in the new angular standalone fashion:
bootstrapApplication(AppComponent, appConfig)
.catch((err) => console.error(err));
The problem is that HttpClient now calls api twice. The first time with no ApiKey header and the second one with the header. It seems like HttpClient is calling the first time without interceptor and the second one with it. If I replace
provideHttpClient(withInterceptors([authInterceptor]))
with
provideHttpClient()
The interceptor doesn't get called and the api is called only once, so I supose the problem is in the interceptor or in the way angular calls the interceptor. Any sugestion? Thanks