Fetch Data from an API using the HttpClient in Angular

Share this video with your friends

Send Tweet

One of the fundamentals in a web application is to learn how to communicate with the backend. In this lesson we learn how to install, configure and use Angular’s HttpClient introduced in version 4.3.1 to fetch data from some backend API.

Mac
Mac
~ 7 years ago

i want to keep this JSON file (people.json) in different folder other then /assets/data. getting 404.

Juri Strumpflohner
Juri Strumpflohner(instructor)
~ 7 years ago

That depends on the setup. The one I'm using here uses the Angular CLI which by default exposes the assets/ folder when running the app. That folder is meant to keep static assets like images and other stuff the app needs to include. I regularly use it for "mock" data like the people.json in this example when you don't have a backend server at hand which serves you a real API.

I'd suggest to stick with the assets folder, but if you want to use a different one, you can totally configure it in the .angular-cli.json file: https://github.com/juristr/egghead-learn-http-in-angular/blob/fetch-data-from-api/.angular-cli.json#L10-L13

Konstantinos
Konstantinos
~ 6 years ago

I want to return an Observable<Person> from the service file, how can I do this?

Konstantinos
Konstantinos
~ 6 years ago

Please delete my comment above.