Angular 4.3.3 HttpClient : How get value from the header of a response?
main problem of typecast so we can use "response" as 'body'
we can handle like
const options = {
headers: headers,
observe: "response" as 'body', // to display the full response & as 'body' for type cast
responseType: "json"
};
return this.http.post(sessionUrl, body, options)
.subscribe(response => {
console.log(response);
return response;
}, err => {
throw err;
});
You can observe the full response instead of the content only. To do so, you have to pass observe: response
into the options
parameter of the function call.
http
.get<MyJsonData>('/data.json', {observe: 'response'})
.subscribe(resp => {
// Here, resp is of type HttpResponse<MyJsonData>.
// You can inspect its headers:
console.log(resp.headers.get('X-Custom-Header'));
// And access the body directly, which is typed as MyJsonData as requested.
console.log(resp.body.someField);
});
See HttpClient's documentation
Indeed, the main problem was a Typescript problem.
In the code of post(), options was declared directly in the parameters, so, as an "anonymous" interface.
The solution was to put directly the options in raw inside the parameters
http.post("url", body, {headers: headers, observe: "response"}).subscribe...