Natixco
Natixco

Reputation: 661

Angular can't set http header, just only with interceptor

I use http interceptor to add an auth token to every request, but for one request I have to add another token insted of the auth one. My problem is that I can't set header for the request.

I tried with these, but none of them are worked correctly. These set the header in the payload, not in the actual header.no

header = new HttpHeaders({'Content-Type': 'application/json'});
header2: HttpHeaders = new HttpHeaders();

constructor(private httpClient: HttpClient) { 
   this.header2.append('Authorization', this.getRefreshToken());
}

ngOnInit() {
   this.httpClient.post('http://localhost:3001/api/token-exchange', { headers: this.header2}).subscribe()
}

Even if it would work, the interceptor would overwrite it, so I tried to use HttpBackend, but that didn't work too.

constructor(
    private httpClient: HttpClient,
    handler: HttpBackend
  ) {
    this.httpClient = new HttpClient(handler);
    this.header2 = this.header2.set('Authorization', this.getRefreshToken());
  }

Upvotes: 0

Views: 57

Answers (1)

CaeSea
CaeSea

Reputation: 298

One way to stop this would be to check the URL of the http request within the interceptor and exclude this URL from the normal logic you would apply. Then add the required headers within the http call as normal.

Upvotes: 1

Related Questions