How do you access the responses on the http response body?
I wrote the following code in Angular 2:
this.http.request('http://thecatapi.com/api/images/get?format=html&results_per_page=10').
subscribe((res: Response) => {
console.log(res);
})
When I print the response I get in console:
I want to have access to the body field in the response code The body field starts with an underscore which means that it's a private field When I change it to 'console.log(res._body)' I got an error.
Do you know the getter function that can help me here?
Best Answer
Both Request
and Response
extend Body
.
To get the contents, use the text()
method.
this.http.request('http://thecatapi.com/api/images/get?format=html&results_per_page=10')
.subscribe(response => console.log(response.text()))
This api was deprecated in angular 5 The new HttpResponse<T>
class instead has a .body()
method. With a {responseType: 'text'}
that should return a String
.