Angular 4.3.3 Httpclient:How Get Value from the Header of a Response

Angular 4.3.3 HttpClient : How get value from the header of a response?

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

Using .pipe() to retrieve response headers in Angular

You can do your local storage code inside the pipe as below.

     createVisitor() {
// Create the visitor to send to API
// TODO: Capture the params of the URL and not the full URL
this.visitor = {
BrandCode: 'honey',
QueryString: 'example=qstring'
};

// Set Token as Session & Return a Success/Fail
return this.http.post(this.api + this.controller + 'createvisitor', this.visitor, {observe: 'response'}).pipe(
map((response: any) => {
console.log(response.headers.keys());// all header names
console.log(response.body); // response content

})
);
}


Related Topics



Leave a reply



Submit