This question already has an answer here:
- duplicate ajax calls in angularjs 2 answers
After adding headers inside code duplicate call is happening. find the image to see the call happening twice.
auth-interceptor.ts
export class AuthInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const clonedRequest = req.clone({
headers: req.headers.set('X-CustomAuthHeader', 'some-auth-token')
});
console.log("new headers", clonedRequest.headers.keys());
return next.handle(clonedRequest);
}
}
The first call is triggered by Cross-Origin Resource Sharing (CORS)
See also helpful article for more information.
This type of request is called Preflighted requests that corresponds to a negotiation between the caller and the Web application based on HTTP headers.
It consists of two phases:
The browser executes an OPTIONS request with the same URL as the target request to check that it has the rights to execute the request. This OPTIONS request returns headers that identify what is possible to do for the URL.
If rights match, the browser executes the request.
Reference here.