Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get authorization header from Http Interceptor

I have made authorization which puts the token to headers directly. How can I now get this token from Angular 4 Http Interceptor?

enter image description here

Unfortunately none of below console logs includes this header:

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    //const changedReq = req.clone({headers: req.headers.set('Authorization', 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbkB3cC5wbCIsImV4cCI6MTUxMDY2NDM0M30.0iBktdr4-1EzTi1iaQOOfguK7HGVJF7JYkB-AF3uZgJrmKnVESAyKkHoNRzum1Pq5xZ6GJaZC9cbZQ2umMSfLA')});
    console.log('req', req);
    return next.handle(req).do((event: HttpEvent<any>) => {
      console.log('event', event);
      if (event instanceof HttpResponse) {
        // do stuff with response if you want
      }
    }, (err: any) => {
      if (err instanceof HttpErrorResponse) {
          this.ehs.setService(err.status, err.error);
          // redirect to login
      }
    });
  }
like image 631
Jan Testowy Avatar asked Jun 16 '26 08:06

Jan Testowy


1 Answers

HttpHeaders is a wrapper for Map instance, so existing headers won't be displayed in console.log output, because Map values aren't exposed.

Headers can be retrieved from HttpHeaders instance, as its API suggests:

req.headers.get('authorization');
like image 180
Estus Flask Avatar answered Jun 18 '26 21:06

Estus Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!