Recover Header from an angular Http Response

Asked

Viewed 435 times

1

I need to recover the token that comes in Authorization and save in localstorage, but, I’m not getting.

Token para recuperação

My login method within my service is:

public login(email: string, password: string): Observable<User> {
        return this.http.post<User>(this.url, { username: email, password: password })
            .do(user => {
                this.user = user;
            });
    }

I believe this information I can retrieve from Subscriber:

public login(): void {
        this.signinService.login(this.loginForm.value.email, this.loginForm.value.password).subscribe(
            user => {
                this.notificationService.notify(`Usuário logado com sucesso`);
            },
            (response: HttpErrorResponse) => this.notificationService.notify(response.error.message),
            () => {
                this.router.navigate([atob(this.navigateTo)]);
            });
    }
  • give a look here https://stackoverflow.com/questions/50968674/angular-6-get-response-headers-with-httpclient-issue

1 answer

0

You can read the header return this way:

http
  .get<any>('url', {observe: 'response'})
  .subscribe(resp => {
    console.log(resp.headers.get('authorization'));
  }); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.