If I have an interceptor that adds headers for each request, but in each request I must add additional headers specific to each request, how can I add those headers?
(I omit imports of httpClient and others)
// any.interceptor.service.ts
intercept(req: HttpRequest<any>, next: HttpHandler) {
const authToken = this.auth.getAuthorizationToken();
const authReq = req.clone({
headers: req.headers.set('Authorization', authToken)
});
return next.handle(authReq);
}
// my.service.ts
@Injectable({
provideIn: 'root'
})
export class MyService {
send(data: AnyInterface) {
// *******************
// adding HERE additional specific headers
// *******************
this.http.post(URL, data);
}
}
I was finally able to resolve the issue. Reading this article I was able to understand that HttpHeaders are immutable, so the solution to @Emilien would only have to modify the following:
send(data: AnyInterface) {
const headers = new HttpHeaders().set('YOUR_HEADER_KEY', 'YOUR_HEADER_VALUE');
this.http.post(URL, data, { headers });
}
Thanks!