Make HTTP requests in Angular?
Last Updated :
18 Apr, 2024
In Angular, making HTTP requests involves communicating with a server to fetch or send data over the internet. It's like asking for information from a website or sending information to it. Angular provides a built-in module called HttpClientModule, which simplifies the process of making HTTP requests.
With this module, you can easily fetch data from a server, send data to a server, and handle responses. These requests are commonly used to interact with APIs (Application Programming Interfaces) to get or update data in web applications.
Steps to Make HTTP requests in Angular
Step 1: Create a new Angular project
ng new my-angular-app
Step 2: Navigate to the project directory
cd my-angular-app
Step 3: Serve the application
ng serve
Project Structure:

The updated dependencies in package.json file will look like.
"dependencies": {
"@angular/animations": "^16.0.0",
"@angular/common": "^16.0.0",
"@angular/compiler": "^16.0.0",
"@angular/core": "^16.0.0",
"@angular/forms": "^16.0.0",
"@angular/platform-browser": "^16.0.0",
"@angular/platform-browser-dynamic": "^16.0.0",
"@angular/router": "^16.0.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.13.0"
}
Approach 1: Using HttpClient Module
Angular provides a built-in HttpClientModule that offers a simplified API for HTTP requests. This module is part of @angular/common/http package. You can import HttpClientModule in your Angular application's root module (AppModule) and then inject HttpClient service where you need to make HTTP requests. It supports various HTTP methods such as GET, POST, PUT, DELETE, etc.
Example : HTTP request using HttpClient Module
HTML
<!-- app.component.html -->
<div class="container" *ngIf="(data$ | async) as data">
<h1>GeeksforGeeks</h1>
<div class="user-container" *ngFor="let user of data.users">
<div class="user-card">
<img [src]="user.image" alt="User Image">
<p>Name: {{ user.firstName }} {{ user.lastName }}</p>
<p>Age: {{ user.age }}</p>
<p>Email: {{ user.email }}</p>
<p>Phone: {{ user.phone }}</p>
</div>
</div>
</div>
CSS
/* App.component.css */
.container {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.user-container {
margin: 20px;
}
.user-card {
text-align: center;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
width: 200px;
}
JavaScript
//app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
JavaScript
//app.component.ts
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
data$: Observable<any>;
constructor(private http: HttpClient) {
this.data$ = this.http.get('https://dummyjson.com/users');
}
}
Output:

Approach 2: UsingThird-party Libraries
You can use third-party libraries like axios, fetch, or rxjs/ajax for making HTTP requests in Angular. For example, you can use the axios library by installing it via npm and then using it in your Angular project.
Example : In this example, we'll use third party libraries like axios for request
HTML
<!-- app.component.html -->
<div class="container" *ngIf="quotes">
<h1>Quotes from Server</h1>
<div class="quote-container" *ngFor="let quote of quotes">
<div class="quote-card">
<p>{{ quote.quote }}</p>
<p>- {{ quote.author }}</p>
</div>
</div>
</div>
CSS
/* App.component.css */
.container {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.quote-container {
margin: 20px;
}
.quote-card {
text-align: center;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
width: 200px;
}
JavaScript
// app.component.ts
import { Component, OnInit } from '@angular/core';
import axios from 'axios';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
quotes: { id: number; quote: string; author: string }[] = [];
ngOnInit() {
this.getQuotesFromServer();
}
async getQuotesFromServer() {
try {
const response = await axios.get('https://dummyjson.com/quotes');
this.quotes = response.data.quotes;
} catch (error) {
console.error('Error fetching quotes:', error);
}
}
}
JavaScript
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Output:

Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read