Sitemap
Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Angular Http Client

--

The Http Client is used to communicate with external endpoints via angular components. Mainly, we use them to fetch data from external endpoints and post data to external endpoints.

Press enter or click to view image in full size

The following Medium blog will help you understand the basic concepts of Angular and set up an Angular application.

Let’s discuss the Http client with the following example.

Get Sachith Ariyathilaka’s stories in your inbox

Join Medium for free to get updates from this writer.

Step 01: This example is regarding the simple user-saving process with the Http Client. At first, we need to import the HttpClientModule to the AppModule as followings.

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

Step 02: Then we have to implement the service layer of the HTTP service. Let’s create a service directory as followings.

Step 03: Now we can implement the movie service class as a service layer. Please note that we’ll use the MovieDB API service as our API provider. I’ll share the link to that API provider as follows.

import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {ApiResponse} from "../resource/api-response";
import {getMoviesUrl} from "../resource/constant";

@Injectable({providedIn: 'root'})
export class MovieService {

constructor(private http: HttpClient) {
}

loadMovies(page: number) {
return this.http.get<ApiResponse>(getMoviesUrl + page);
}

}
export class ApiResponse {
page = 0;
result = [];
total_pages = 0;
total_results = 0;
}
export const getMoviesUrl = 'https://api.themoviedb.org/3/movie/popular?api_key={api-key}&language=en-US&page='

Step 04: Finally, we can subscribe to the data load method of the movie service class from the component class as follows.

import {Component, OnInit} from '@angular/core';
import {MovieService} from "../service/movie.service";

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
constructor(private movieService: MovieService) {}

ngOnInit(): void {
this.movieService.loadMovies(1).subscribe(response => {
alert("Movies count is: " + response.results.length)
}, (error: any) => {
console.log(error);
});
}
}

We can perform suitable tasks under the success method or failed method in the subscriber body. In this example, we just included the console commands for success response and error response. By using this structure, we can connect any type of REST endpoint (POST, GET, PUT, etc.) with our Angular application using the Http Client. The following GitHub link will help you find the source code of this blog.

--

--

Towards Dev
Towards Dev

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.