How to call HttpClient without HttpInterceptors?


I followed the spa angular tutorial to authenticate my user, all works fine but i dont want to use the HttpInterceptor, instead i want to inject the access token myself when needed.

So I try something like this :
get(atUrl: string): Observable {
return this.authService.getTokenSilently$().pipe(
concatMap(token => {
let headers = this.buildHTTPHeader(token);
let url = myurl;
return this.http.get(url, {
headers: headers
catchError(err => throwError(err))

Can you confirm that the best method to manage token manually in httpClient ?


Howdy @luc.nempont :cowboy_hat_face:

Yes, attaching your token to the request this way ought to be totally fine (assuming your headers also include the content-type).

The reason we’d use the interceptor is because it’s often a much simpler way to append a token to an outgoing request. Your solution has the tradeoff of needing to explicitly define headers each request.

A guard clause could also be placed in the interceptor logic to control when headers are applied based on the HttpRequest meeting certain conditions.

I hope this helps answer your question?

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.