In this section, we will learn how to make HTTP requests in Angular using the HttpClient
module. This is a crucial part of any web application that needs to communicate with a backend server to fetch or send data.
Key Concepts
- HttpClient Module: The Angular module that provides a simplified API for HTTP requests.
- HttpClient Methods: Methods such as
get
,post
,put
,delete
, etc., used to perform HTTP operations. - Observables: Used to handle asynchronous data streams in Angular.
Setting Up HttpClient
Before making HTTP requests, you need to import the HttpClientModule
in your Angular application.
-
Import HttpClientModule:
import { HttpClientModule } from '@angular/common/http';
-
Add HttpClientModule to AppModule:
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { HttpClientModule } from '@angular/common/http'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, HttpClientModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Making a GET Request
The HttpClient
service is used to make HTTP requests. Let's start with a simple GET request to fetch data from a server.
-
Inject HttpClient in a Service:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class DataService { private apiUrl = 'https://api.example.com/data'; constructor(private http: HttpClient) { } getData(): Observable<any> { return this.http.get<any>(this.apiUrl); } }
-
Use the Service in a Component:
import { Component, OnInit } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-data', template: ` <div *ngIf="data"> <pre>{{ data | json }}</pre> </div> ` }) export class DataComponent implements OnInit { data: any; constructor(private dataService: DataService) { } ngOnInit(): void { this.dataService.getData().subscribe(response => { this.data = response; }); } }
Making a POST Request
To send data to the server, you can use the post
method.
-
Service Method for POST Request:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class DataService { private apiUrl = 'https://api.example.com/data'; constructor(private http: HttpClient) { } postData(data: any): Observable<any> { return this.http.post<any>(this.apiUrl, data); } }
-
Use the Service in a Component:
import { Component } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-data', template: ` <button (click)="sendData()">Send Data</button> ` }) export class DataComponent { constructor(private dataService: DataService) { } sendData(): void { const data = { name: 'Angular', version: '2+' }; this.dataService.postData(data).subscribe(response => { console.log('Data sent successfully', response); }); } }
Handling Errors
It's important to handle errors that may occur during HTTP requests. You can use the catchError
operator from RxJS.
-
Service Method with Error Handling:
import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class DataService { private apiUrl = 'https://api.example.com/data'; constructor(private http: HttpClient) { } getData(): Observable<any> { return this.http.get<any>(this.apiUrl).pipe( catchError(this.handleError) ); } private handleError(error: HttpErrorResponse) { let errorMessage = 'Unknown error!'; if (error.error instanceof ErrorEvent) { // Client-side errors errorMessage = `Error: ${error.error.message}`; } else { // Server-side errors errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`; } return throwError(errorMessage); } }
-
Component with Error Handling:
import { Component, OnInit } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-data', template: ` <div *ngIf="data"> <pre>{{ data | json }}</pre> </div> <div *ngIf="error"> <p>{{ error }}</p> </div> ` }) export class DataComponent implements OnInit { data: any; error: string; constructor(private dataService: DataService) { } ngOnInit(): void { this.dataService.getData().subscribe( response => { this.data = response; }, error => { this.error = error; } ); } }
Practical Exercise
Exercise: Fetch and Display Data
- Objective: Create a service to fetch data from a public API and display it in a component.
- Steps:
- Create a new service called
UserService
. - Use the
HttpClient
to fetch data fromhttps://jsonplaceholder.typicode.com/users
. - Create a component called
UserComponent
to display the fetched data.
- Create a new service called
Solution
-
UserService:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class UserService { private apiUrl = 'https://jsonplaceholder.typicode.com/users'; constructor(private http: HttpClient) { } getUsers(): Observable<any> { return this.http.get<any>(this.apiUrl); } }
-
UserComponent:
import { Component, OnInit } from '@angular/core'; import { UserService } from './user.service'; @Component({ selector: 'app-user', template: ` <div *ngIf="users"> <ul> <li *ngFor="let user of users">{{ user.name }}</li> </ul> </div> ` }) export class UserComponent implements OnInit { users: any; constructor(private userService: UserService) { } ngOnInit(): void { this.userService.getUsers().subscribe(response => { this.users = response; }); } }
Conclusion
In this section, we learned how to make HTTP requests using the HttpClient
module in Angular. We covered GET and POST requests, error handling, and provided a practical exercise to reinforce the concepts. In the next section, we will learn how to handle HTTP responses effectively.
Angular 2+ Course
Module 1: Introduction to Angular
Module 2: TypeScript Basics
- Introduction to TypeScript
- TypeScript Variables and Data Types
- Functions and Arrow Functions
- Classes and Interfaces