How to send REST response to html in Angular ?
Last Updated :
31 Jul, 2024
In this article, we will see how to send API responses using HttpClient Module to an HTML using Angular, along with understanding the basic implementation through the examples.
Angular is a JavaScript framework through which we can create reactive single-page web applications. For implementation, we are using the latest version of Angular (Angular 14) to handle HTTP. For Angular applications, the HTTP Client in @angular/common/HTTP provides a simplified client HTTP API that is based on the XMLHttpRequest interface provided by browsers. The testability features, typed request and response objects, request and response interception, Observable APIs, and simplified error handling are some further advantages of HttpClient. To retrieve data from the server and send it to our application, we need an HTTP client.
Before we proceed with the installation process, we should have NodeJS & NPM (Node Package Manager) installed in our system. If not, please refer to the Installation of Node.js on Windows or Linux article for the detailed installation procedure.
Steps for sending the REST response in Angular:
Step 1: Angular Installation using NPM
Install Angular with the help of NPM to install:
npm install -g @angular/cli
Step 2: Create a New Project
In angular, we need to use ng new project_name for creating a new project.
ng new api_response
Creating new projectStep 3: Importing HttpClientModule for request and response
For Configuring HttpClient, we need to Import the HttpClientModule into src/app/app.module.ts.
JavaScript
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule }
from '@angular/common/http';
import { AppComponent } from './app.component';
@NgModule({
imports: [BrowserModule,
FormsModule,
HttpClientModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
Step 4: Using HttpClient in the app.component.ts file
We need to use HttpClient for requesting an API Call for getting some data in response. First, We are creating private variable httpClient of HttpClient type using the constructor. Then, create one method for calling API using the GET method and storing the response in the results variable. Calling this method in ngOnInit() for loading gets a response when the page loads.
JavaScript
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'GeeksForGeeks API Response';
results: any;
constructor(private http: HttpClient) {
}
ngOnInit(): void {
this.getApiResponse();
}
getApiResponse() {
this.http.get(
// API Link
'https://.../products').subscribe((response) => {
this.results = response;
});
}
}
Step 5: Showing Data in Table format
We are using fake store API for getting a list of products or an array of products. Each product has some properties such as id, title, price, description, category, and rating. Below, add the image of one product object.
Product Object ImageWe have an array of products with similar product properties. Using ngFor loop for showing response data in HTML file. We are using the below-mentioned API for getting data.
API Link: You can use any API link. For example - https://.../products
HTML
<div class="container mt-5">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Title</th>
<th scope="col">Price</th>
<th scope="col">Description</th>
<th scope="col">Category</th>
<th scope="col">Rating</th>
</tr>
</thead>
<tbody>
<ng-container *ngFor="let result of results">
<tr>
<th scope="row">{{ result.id }}</th>
<td>{{ result.title }}</td>
<td>{{ result.price }}</td>
<td>{{ result.description }}</td>
<td>{{ result.category }}</td>
<td>{{ result.rating['rate'] }}</td>
</tr>
</ng-container>
</tbody>
</table>
</div>
Output:
Similar Reads
How to Send an HTTP POST Request in JS?
We are going to send an API HTTP POST request in JavaScript using fetch API. The FetchAPI is a built-in method that takes in one compulsory parameter: the endpoint (API URL). While the other parameters may not be necessary when making a GET request, they are very useful for the POST HTTP request. Th
2 min read
How to reuse template HTML block in Angular ?
Code Reusability is the capacity to repurpose pre-existing code when developing new software applications. It will allow you to store a piece of code that does a single task inside a defined block, and then call that code whenever you need it. In this article, we will learn How to reuse template HTM
2 min read
How to use services with HTTP methods in Angular v17?
In Angular 17, services play an important role in managing HTTP requests to backend servers. By encapsulating HTTP logic within services, you can maintain clean, modular, and testable code. In this article, we'll explore how to use services to handle HTTP requests effectively in Angular 17 applicati
3 min read
How To Use HttpClient in Angular?
In Angular, the HttpClient module is used to make HTTP requests to backend services. It simplifies communication with APIs, allowing developers to interact with RESTful services, send and receive data, and handle responses effectively. This article will guide you through setting up HttpClient, makin
6 min read
How to Subscribe to an Observable in Angular?
In Angular, managing data streams effectively is important, especially when dealing with asynchronous operations. Observables, a core part of the RxJS library used in Angular, are powerful tools for handling these data streams. In this article, we will explore how to subscribe to Observables in Angu
4 min read
How to call web services in HTML5 ?
In this article, we will see how to call the Web Services in HTML5, along with knowing different methods for calling the web services & understand them through the examples. Web services in HTML5 are a set of open protocols and standards that allow data to be exchanged between different applicat
3 min read
How to Enable HTML 5 Mode in Angular 1.x ?
HTML5 mode in Angular1.x is the configuration that replaces the default hash-based URLs and provides more user-friendly URLs using the HTML5 History API. This feature mainly enhances our one-page application and also provides a better experience to the users. We need to configure our server with pro
6 min read
How to create and send POST requests in Postman?
Postman is an API(application programming interface) development tool which helps to build, test and modify APIs. It can make various types of HTTP requests(GET, POST, PUT, PATCH), saving environments for later use, and convert save the API to code for various languages(like JavaScript, and Python).
2 min read
How to insert HTML into view from AngularJS controller?
The ng-bind-html directive is a secure way of binding content to an HTML element. So in order to insert HTML into view, we use the respective directive. While using AngularJS, write HTML in our corresponding application, we should check the HTML for dangerous or error prone code. By including the "a
2 min read
How to Retrieve Data using HTTP with Observables in Angular ?
Most applications obtain data from the backend server. They need to make an HTTP GET request. In this article, we'll look at making an HTTP request and map the result/response in a local array. This array can be used to display or filter the items as we want. The most important thing here is using O
4 min read