Angular 15 RxJS Observable with Httpclient Example

By Hardik Savani April 4, 2023 Category : Angular

Hey Dev,

This tutorial will provide an example of angular 15 observable example. step by step explain angular 15 rxjs observable example. you will learn angular 15 http observable example. This example will help you angular 15 observable with httpclient example.

If you don't know how to use observable with httpclient request in an angular application then I will help you get done. we always prefer to use observable for HTTP requests that help to manage service requests and monitor server requests. observable is provided by rxjs.

Here, I will give you a very simple example of an HTTP request with observable in angular. we will use JSON placeholder API to make API requests. so let's follow some steps to get the example done, I also attach a preview at the bottom.

Step 1: Create New App

You can easily create your angular app using the below 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 below:


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 { }

Step 3: Create Post Class

In this step, we will simply create a Post class and define data types of returning data. so let's create a post.ts file and put the 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 a service for HTTP client requests. we will create a service file and write a client http request using observable code. this service will use in our component file. So let's create a service and put the bellow code:

ng g s post

Now let's add the code as like below:


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 these services for our app component. So let's update the code as like below:


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 update our HTML file. let's put the bellow code:

I used bootstrap class on this form. if you want to add that then follow this link too: Install Bootstrap 5 to Angular 15.


<div class="container">

<h1>Angular 15 Observables HttpClient Example -</h1>

<table class="table table-bordered">







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

<td>{{ }}</td>

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

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

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




Run Angular App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Angular app:

ng serve

Now, Go to your web browser, type the given URL and view the app output:


You can simply see the preview below:

I hope it can help you...