By default, the response body doesn ' t contain all of the data that might is needed in your app. Your server might return some special header which you had to read explicitly. In such case we can use the { observe: ‘response’}
configuration of the Angular HttpClient
. Let ' s explore.
Import {injectable} from ' @angular/core 'rxjs/observable 'httpresponse' @angular/common/http ' ; Export interface Person { name:string;} @Injectable () Export class Peopleservice { constructor (private http:httpclient) {} fetchpeople (): Observable <httpresponse<Person>> { returnthis. http . Get {observe: ' response '});} }
Now instead of just returning your data, it returns your response object.
{ "headers": { "normalizednames": [], null }, " Status ": $, " statustext ":" OK ", " url ":" https://run.plnkr.co/preview/ Cjdn2x8fh000ffillqi8d3o4k/data/people.json ", true, " type ": 4, "Body": [ { "name": "XXX" }, { "name": "XXX" } ]}
[Angular] Read Custom HTTP Headers Sent by the Server in Angular