Angular HttpClient with Observable Example

Angular HttpClient with Observable Example

In this post we will give you information about Angular HttpClient with Observable Example. Hear we will give you detail about Angular HttpClient with Observable ExampleAnd how to use it also give you demo for it if it is necessary.

Now, let’s see example of angular observable http request example. step by step explain angular httpclient request with observable example. i would like to show you angular httpclient with rxjs observable example. it’s simple example of angular observable http get example. follow bellow step for observable http request angular.

we will see example of httpclient with rxjs observable in angular and you can easily use with angular 6, angular 7, angular 8 and angular 9 application

If you don’t know how to use observable with httpclient request in angular application then i will help you getting done. we always prefer to use observable for http request that help to manage server request and monitor to server request. observable is provided by rxjs.

Here, i will give you very simple example with http request with observable in angular. we will use jsonplaceholder api to make api request. so let’s follow some step to get example done, i also attach preview on bottom.

Step 1: Create New App

You can easily create your angular app using bellow command:

ng new my-new-app

Step 2: Import HttpClientModule

In this step, we need to import HttpClientModule to app.module.ts file. so let’s import it as like bellow:


import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { HttpClientModule } from '@angular/common/http';


declarations: [



imports: [




providers: [],

bootstrap: [AppComponent]


export class AppModule { }

Also see:Angular Http Post Request Example

Step 3: Create Post Class

In this step, we will simply create Post class and define data types of returning data. so let’s create post.ts file and put bellow code:


export class Post {


public body: string,

public id: number,

public title: string,

public userId: number

) {}


Step 4: Create Service for Call API

Here, we need to create service for http client request. we will create service file and write client http request using observable code. this service will use in our component file. So let’s create service and put bellow code:

ng g s post

Now let’s add code as like bellow:


import { Injectable } from '@angular/core';

import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs';

import { Post } from './post';


providedIn: 'root'


export class PostService {

private url: string = '';

constructor(private httpClient: HttpClient) { }

public getPosts(): Observable<Post[]>{

return this.httpClient.get<Post[]>(this.url);



Step 5: Use Service to Component

Now we have to use this services to our app component. So let’s updated code as like bellow:


import { Component, OnInit } from '@angular/core';

import { PostService } from './post.service';

import { Post } from './post';


selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']


export class AppComponent implements OnInit {

name = 'Angular';

posts = new Array<Post>();

constructor(private service:PostService) {}

ngOnInit() {

this.service.getPosts().subscribe(response => {

this.posts = =>


return new Post(









Step 6: Updated View File

Now here, we will updated our html file. let’s put bellow code:

I used bootstrap class on this form. if you want to add than then follow this link too: Install Boorstrap 4 to Angular 9.


<div >

<h1>Angular 8/9 Observables HttpClient Example -</h1>

<table >







<tr *ngFor="let post of posts">

<td>{{ }}</td>

<td>{{ post.body }}</td>

<td>{{ post.title }}</td>

<td>{{ post.userId }}</td>




Now we are ready to run our example, you can run by following command:

Also see:How to Display Data from Json File in Angular?

ng serve

You can simply see preview as like bellow:

I hope it can help you…

Hope this code and post will helped you for implement Angular HttpClient with Observable Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

4  +  6  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US