Here is an example of a get
http call:
this.http
.get('http://thecatapi.com/api/images/get?format=html&results_per_page=10')
.map(this.extractData)
.catch(this.handleError);
private extractData(res: Response) {
let body = res.text(); // If response is a JSON use json()
if (body) {
return body.data || body;
} else {
return {};
}
}
private handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
Note .get()
instead of .request()
.
I wanted to also provide you extra extractData
and handleError
methods in case you need them and you don't have them.