ItSolutionStuff.com

Angular 18 HttpClient & Http Services Tutorial

By Hardik Savani • July 4, 2024
Angular

In this post, I would like to share with you a simple example of Angular 18 HttpClient & Http Services example.

It's crucial for every app to send API requests to other servers. Whether you're working with Angular, Vue, or React applications, knowing how to execute HTTP client requests is essential. In this guide, I'll show you a super simple example of making API requests to get and store data. For testing, we'll use the JSON placeholder API, so you don't have to create a new one.

Step for Create HttpClient & Http Service in Angular 18

  • Step 1: Create Angular 18 Project
  • Step 2: export provideHttpClient()
  • Step 3: Create Service for API
  • Step 4: Use Service to Component
  • Step 5: Updated View File
  • Run Angular App

So, let's see the below example step on step how to create an HTTP service and how to use it.

Step 1: Create Angular 18 Project

You can easily create your angular app using below command:

ng new my-new-app

Step 2: export provideHttpClient()

In this step, we need to export provideHttpClient() to app.config.ts file. so let's import it as like bellow:

src/app/app.config.ts

import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
  
import { routes } from './app.routes';
import { provideAnimations } from '@angular/platform-browser/animations';
  
import { provideHttpClient } from '@angular/common/http';
   
export const appConfig: ApplicationConfig = {
  providers: [provideRouter(routes), provideAnimations(), provideHttpClient()]
};

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 services/post

Now let's add code as like bellow:

src/app/services/post.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
  
@Injectable({
  providedIn: 'root'
})
export class PostService {
  private url = 'http://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 } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
  
import { PostService } from './services/post.service';
  
@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, RouterOutlet],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    posts:any;
  
    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 18 HttpClient for Sending Http Request Example - ItSolutionStuff.com</h1>
  
<ul class="list-group">
  <li 
      *ngFor="let post of posts"
      class="list-group-item">
      {{ post.title }}
  </li>
</ul>

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:

http://localhost:4200

you will see the layout below:

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 Define Global Variables in Angular 18?

Read Now →

Angular 18 Template Driven Form with Validation Example

Read Now →

Angular 18 Multiple Image Upload Example Tutorial

Read Now →

Angular 18 File Upload Tutorial Example

Read Now →

Angular 18 Image Upload with Preview Example

Read Now →

Angular 18 Reactive Forms with Validation Example

Read Now →

How to Add Bootstrap 5 in Angular 18 Application?

Read Now →

How to Update Angular 17 to Angular 18 Version?

Read Now →

Angular Material Checkbox Change Size Example

Read Now →

Angular Material Multi Select Dropdown Example

Read Now →

Angular Material Carousel Slider Example

Read Now →

Angular Material Input Autocomplete Example

Read Now →