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
How to Tell If a <Script> Tag Failed to Load
What Is the Lifecycle of an Angularjs Controller
Jquery or JavaScript to Find Memory Usage of Page
Event.Preventdefault() VS. Return False (No Jquery)
Javascript: Undefined !== Undefined
Create an Object from an Array of Keys and an Array of Values
How to Get the Browser Language Using JavaScript
Download a Div in a HTML Page as PDF Using JavaScript
Resolving Require Paths with Webpack
Differencebetween Regexp's Exec() Function and String's Match() Function
Jquery Beforeunload When Closing (Not Leaving) the Page
Finding All Indexes of a Specified Character Within a String
How to Export Tables to Excel from a Webpage
Angular: Can't Find Promise, Map, Set and Iterator
Detect If Hovering Over Element with Jquery
How to Invert (Transpose) the Rows and Columns of an HTML Table