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-
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);
}
);
}
You are almost there.
The reason it is not working is because you didn't use the .get
function of the headers
.
Change this
console.log(resp.headers);
To this
console.log(resp.headers.get('Authorization'))
More Info:
Official doc here
Are you sure you are adding it as a part of the response? You need to add the header in the response:
public void methodJava(HttpServletResponse response){
...
response.addHeader("access-control-expose-headers", "Authorization");
}
And then you can do what you have been trying. I think headers.get('Authorization') should give you the desired value