Reputation: 2415
I am new to angular. I can not read the token (jwt) in the REST service response header. How can I read headers? this is my code:
loginUser(loginUser:Login,callback: (data) => void){
let body = JSON.stringify(loginUser)
const headerJson = {
'Content-Type': 'application/json'
}
let options = { headers: new HttpHeaders( headerJson ) };
return this.http.post(this.config.datiBean.urlLoginToken, body,options).subscribe(
res =>{
console.log("body :"+JSON.stringify(res)); //ok
callback(res);
}
/// at this point how can I read headers?
)};
}
I would like to read this information:
thanks for your help
Upvotes: 0
Views: 3360
Reputation: 2415
Most likely my method was not correct. I modified my method this way:
loginUser(loginUser:Login,callback: (data) => void){
let creds = JSON.stringify(loginUser);
let contentHeader = new HttpHeaders({ "Content-Type":"application/json" });
this.http.post(this.config.datiBean.urlLoginToken, creds, { headers: contentHeader, observe: 'response' })
.subscribe(
(resp) => {
console.log("TOKEN: "+resp.headers.get('X-Auth'))
console.log("body: "+JSON.stringify(resp.body))
callback(resp)
},
(resp) => {
console.log("resp-error");
console.log(resp);
}
);
};
Now it works correctly
Upvotes: 2
Reputation: 13
Diego.
If you are using the NG4 and the now deprecated http class then I think you should try to collect the keys and values inside the subscription. https://v4.angular.io/api/http/Headers#values
But consider that you will only be able to use the headers that are exposed. You can include the list of those using the Access-Control-Expose-Headers.
res =>{
console.log("body :"+JSON.stringify(res)); //ok
callback(res);
headerKeys = res.headers.keys(); // this will provide an array of strings for the exposed headers
}
Upvotes: 0