ItSolutionStuff.com

Angular HttpClient Get Example | Angular Http Get Request Example

By Hardik Savani • October 20, 2023
Angular

Here, i will show you angular 9 http get request example. step by step explain angular httpclient get request example. I’m going to show you about angular http get service example. I’m going to show you about how to use http get in angular. Let's get started with httpclient get request example angular.

I also write simple example of angular httpclient post request example, you can view from here: Angular Http Client Post Request.

Here, i will give you very simple example to get all data using api using get request api. we will use jsonplaceholder api for testing now. so we don't require to create new api for it.

So, let's see bellow example step by step how to create http service and how to use it.

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:

src/app/app.module.ts

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

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

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

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

@NgModule({

declarations: [

AppComponent

],

imports: [

BrowserModule,

HttpClientModule

],

providers: [],

bootstrap: [AppComponent]

})

export class AppModule { }

Step 3: Create Service for API

Here, we need to create service for http client request. we will create service file and write client http request 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:

src/app/post.service.ts

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

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

@Injectable({

providedIn: 'root'

})

export class PostService {

private url = 'https://jsonplaceholder.typicode.com/posts';

constructor(private httpClient: HttpClient) { }

getPosts(){

return this.httpClient.get(this.url);

}

}

Step 4: Use Service to Component

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

src/app/app.component.ts

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

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

@Component({

selector: 'app-root',

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

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

})

export class AppComponent implements OnInit {

posts;

constructor(private service:PostService) {}

ngOnInit() {

this.service.getPosts()

.subscribe(response => {

this.posts = response;

});

}

}

Step 5: Updated View File

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

src/app/app.component.html

<h1>Angular 8/9 HttpClient Get Example - ItSolutionStuff.com</h1>

<ul class="list-group">

<li

*ngFor="let post of posts"

class="list-group-item">

{{ post.title }}

</li>

</ul>

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

ng serve

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

How to Check Form is Valid or not in Angular?

Read Now →
ā˜…

How to Use Interface in Angular?

Read Now →
ā˜…

Angular Date Pipe Example | Date Pipe in Angular

Read Now →
ā˜…

Angular Currency Pipe Example | Currency Pipe in Angular

Read Now →
ā˜…

Angular NgForm Example | NgForm Directive In Angular

Read Now →
ā˜…

10 Digit Mobile Number Validation in Angular

Read Now →
ā˜…

Angular 9 Service Example | Create Service in Angular 9

Read Now →
ā˜…

Angular 9 Reactive Form Validation Example

Read Now →
ā˜…

Angular Service with Httpclient Example

Read Now →
ā˜…

Angular 9/8 HttpClient for Sending Http Request Example

Read Now →