Professional Documents
Culture Documents
Techiediaries
This tutorial is part of a tutorial series titled ¨Ionic 5, Angular and RxJS Observables: Building an HTTP
Service to Communicate with A REST API¨ that contains the following tutorials:
In this tutorial we'll see how to create an Angular service/provider to encapsulate the code to
communicate with The REST API back-end.
Ionic 5 JWT Authentication Tutorial: Using Angular HttpClient with Node & Express.js Server
Ionic 5 Tutorial: Building and Theming a Login & Register UI with Angular Forms
https://www.techiediaries.com/ionic-http-client/ 1/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
An HTTP Request will take some time to reach the API server and also the HTTP Response will need
time to arrive so this needs to be running in the background before the data can be ready to be consumed.
With Ionic 5/Angular you can make use of modern JavaScript APIs: Promises and Observables which
provide high level abstractions to handle the asynchronous nature of data fetching and API consuming
operations or any other operation that takes time to finish.
What's a Promise?
The Promise object represents the eventual completion (or failure) of an asynchronous operation,
and its resulting value.--MDN
You place your async actions, either when the promise has successfully resolved or failed, within the
.then(()=>{}) and .catch(()=>{}) methods.
Image source
https://www.techiediaries.com/ionic-http-client/ 2/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
What is an Observable?
Observables are a newer standard than promises which are added, to Angular 2+ (and will be included in
the ES7) to allow Angular developers to handle more advanced use cases with clear and concise code. For
example you can cancel an observable whenever you need without using external libraries as the case for
promises and you can also have multiple return values.
Just like Promises, Observables are abstractions that help you deal with async operations except that they
handle asynchronosity in a different way and provide more features so they are becoming preferable over
promises among the JavaScript/Angular community.
Unlike promises, which they can only handle single events, observables, on the other hand, can be passed
more than one event.
An Observable can be represented as a stream of events that can be handled with the same API and they
can be cancelable (this feature is not available for ES6 Promises so you need to use external libraries to do
that).
You can use different array-like operators such as map(), forEach(), and reduce() etc. to easily work with
observables and handle advanced use cases with a simple and clear API.
The new Angular HttpClient methods return observables objects which can be also converted to promises
(using toPromise() operator) so you can use the right abstraction when it's appropriate.
A service provider is an Angular abstraction which can be used in any other component, page or service
via the Angular Dependency Injection or DI. You can use providers to encapsulate code which's common
between many places of your application so instead of repeating the same logic in many places you can
isolate that code into its own service and inject it wherever you want to use it. This will allow you to
comply with the DRY (Don't Repeat Yourself) principle. If you don't know what DRY is, here is its
definition from Wikipedia
In software engineering, don't repeat yourself (DRY) is a principle of software development aimed
at reducing repetition of software patterns, replacing them with abstractions; and several copies of
the same data, using data normalization to avoid redundancy.
By following the DRY principle you place the code which interfaces with your back-end API in one place
which makes the app easy maintainable.
Now let's generate our API-interfacing service using the Ionic CLI 5. Head back to your terminal or
command prompt then run the following command to generate a service provider
https://www.techiediaries.com/ionic-http-client/ 3/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
This command will create a new folder in your project's src/providers, and add your newly created
provider to the array of providers in src/app/app.module.ts (If it's not added make sure to do it
manually).
/* Other imports */
@NgModule({
/* ... */
providers: [
StatusBar,
SplashScreen,
})
The new Angular HttpClient API was introduced in Angular 4.3+. It is a better alternative to the existing
HTTP API that lives in its own package @angular/common/http.
In Angular 5, the old HTTP client which lives in @angular/http is deprecated so Angular and Ionic 5
developers need to migrate their existing apps to use the new HttpClient API.
HttpClient has many changes and features over the old API, such as:
the introduction of the requestProgress interface for listenning for download and upload operations
progress
https://www.techiediaries.com/ionic-http-client/ 4/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
the introduction of the HttpInterceptor interface for creating interceptors--middlewares that can be placed
in the Request/Response pipeline
The Angular HttpClient service is available as an injectable class which can be imported from
@angular/common/http.
HttpClient provides methods, for sending HTTP POST, GET, PUT and DELETE etc. requests, that return
Observables.
Based on the endpoints exposed by our simple json-server back-end, we can create an example
implementation of our Angular service
@Injectable()
baseUrl:string = "http://localhost:3000";
public getProducts(){
https://www.techiediaries.com/ionic-http-client/ 5/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
We have imported the Injectable decorator to transform this TypeScript class into an injectable service.
Then we imported the HttpClient to make the HTTP requests.
Next we have declared the baseUrl variable to hold the address of your back-end API. Next we injected
HttpClient as httpClient.
Before you can succesfully implement the service methods, you need to make sure to import the following
dependencies from the RxJS library:
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
You also need to declare and define a Product model, either in the same file as the service or in a separate
file then import it:
id: number;
name: string;
cost: number;
quantity: number;
Object.assign(this, values);
https://www.techiediaries.com/ionic-http-client/ 6/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
The getProducts() method will be used to get all products from the corresponding API endpoint:
return this.httpClient
.get(this.baseUrl + '/products')
.map(products => {
})
.catch((err)=>{
console.error(err);
});
We first call the .get() method to send the GET request to the corresponding endpoint which will return
an endpoint
We then use the RxJS map() operator in the returned Observable to convert it from Observable to
Observable i.e an array of Products.
return this.httpClient
.map(response => {
})
.catch((err)=>{
https://www.techiediaries.com/ionic-http-client/ 7/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
console.error(err);
});
The createProduct() method will be used to create a new product by sending a POST request, with the
product data, to the corresponding endpoint.
return this.httpClient
.map(response => {
})
.catch((error)=>{
console.error(error);
});
The updateProduct() will be used to update a product by its id, by sending a PUT request to the
corresponding endpoint then will convert the response to a new Product using the RxJS .map() operator.
return this.httpClient
.map(response => {
})
https://www.techiediaries.com/ionic-http-client/ 8/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
.catch((err)=>{
console.error(err);
});
The deleteProductById() method will be used to delete single products by id, by sending a DELETE
request to the corresponding endpoint:
return this.httpClient
.catch((e)=>{
console.error(e);
});
After implementing the service to interface with our REST back-end, let's now see how to use the service
in our app.
All the methods we have previously implemented in the service return RxJS Observables
Calling any method in our components won't send any HTTP requests. We need to subscribe to the
returned Observable to send the corresponding request to the API back-end.
To subscribe to an Observable, we need to use the .subscribe() method, which takes 3 arguments:
https://www.techiediaries.com/ionic-http-client/ 9/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
Use the Ionic CLI 5 to generate a page for adding CRUD (Create, Read, Update and Delete) operations
which will call the corresponding methods in the previously create service.
Then we call this code, to get all products and store them in the products array, when the view enters or in
the constructor:
this.restProvider.getProducts().subscribe((products : Product[])=>{
this.products = products;
});
onCreateProduct() is called when we need to create a product via a form. This method simply
subscribes to the corresponding method in the service and concatenates the newly created product with the
products array
onCreateProduct(product) {
this.restProvider
.createProduct(product)
.subscribe(
https://www.techiediaries.com/ionic-http-client/ 10/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
(newProduct) => {
this.products = this.products.concat(newProduct);
);
onUpdateProduct(product) {
this.restProvider
.updateProduct(product)
.subscribe(
(updatedProduct) => {
/* You can assign back the updated product to the model holding the form's product*/
);
onRemoveProduct() can be called when you need to delete a product. This method the array .filter()
method to filter out the deleted product from the array of products:
onRemoveProduct(product) {
this.restProvider
.deleteProductById(product.id)
.subscribe(
() => {
);
https://www.techiediaries.com/ionic-http-client/ 11/13
21/04/2020 Building an Ionic 5/Angular HTTP Service to Communicate with A REST API | Techiediaries
Concluion
This is the end of this tutorial where we have seen how to make HTTP requests in your Ionic 5/Angular
app. The next tutorial in this tutorial series is: Unit Testing Angular Services with HttpTestingController
Sponsored Links
https://www.techiediaries.com/ionic-http-client/ 13/13