How to access headers from a HttpClient response? (Angular / Ionic)

后端 未结 3 789
面向向阳花
面向向阳花 2020-12-19 13:36

I\'m using a login endpoint that returns a bearer token as a response header, as I can see in the \"Network\" Chrome inspect window:

Response Headers
Access-         


        
3条回答
  •  抹茶落季
    2020-12-19 14:15

    try like this :

    authenticate(credentials) {
        let creds = JSON.stringify(credentials);
        let contentHeader = new HttpHeaders({ "Content-Type": "application/json" });
        this.http.post(this.LOGIN_URL, creds, { headers: contentHeader, observe: 'response' })
            .subscribe(
            (resp) => {
                let header: HttpHeaders = resp.headers;
                console.log(header.get('Authorization'))
            },
            (resp) => {
                console.log("resp-error");
                console.log(resp);
            }
            );
    }
    

提交回复
热议问题