Making GET requests

The HTTP client is available as Http service in the @angular/http package; import it in our component and inject it via dependency injection into a constructor:

import { Http } from '@angular/http'; constructor(private http: Http) { }   

We have our HTTP client; now invoke the API to get the data whenever the user clicks on the Get Books Data button:

getBooksData() {     this.http.get('http://localhost:4567/books')         .subscribe(res => this.booksList =                             res.json() as Book[]); } 

We are calling our API using the GET method. Angular Http service is an Observable, and we need to subscribe to it to receive the response. Once the preceding code is added to the getBooksData() method, if we click on the button, we will receive all ...

Get Building Modern Web Applications using Angular now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.