Exploring HTTP Clients: Axios, Requests, and Node-Fetch

Search for a command to run...

No comments yet. Be the first to comment.
Nitro is a server toolkit used by many JavaScript applications. You can use it on its own for APIs or full-stack servers, or combine it with Vite to get a familiar frontend workflow with a Nitro serve

All durable execution engines give the same versioning advice: pin your runs. Temporal pins runs to worker builds. Vercel's Workflow SDK pins each run to the deployment that started it. Azure Durable

Eve organizes AI agents in a simple way: use Markdown for instructions and skills, TypeScript for tools, and separate files for channels, schedules, and subagents. Behind this setup, Eve relies on the

Picture an online store launching a new product and sending out a mailing list campaign. Thousands of users click the same link at once. The product page, built with a Node.js app like Next.js, needs

How Platformatic ICC Outperforms AWS ECS Target Tracking and Step Scaling

When building dynamic web applications with Node.js, the choice of HTTP client is pivotal for effective communication with web APIs and efficient handling of website queries.
Selecting the right HTTP client for your project can significantly influence productivity and usability. A well-chosen HTTP client can optimize resource utilization, speed up execution, and elevate the readability and maintainability of your codebase.
In this article, we will delve into various HTTP clients available and key considerations for selecting the ideal one for your application.
As of 2023, the landscape of popular HTTP clients included Axios, Got, Node-Fetch, Request, and Undici.
Axios continues to reign supreme, while Node-Fetch gains traction steadily. Meanwhile, Got maintains its ground within the Node.js ecosystem, whereas Request is witnessing a gradual decline in usage, as shown below:
Let’s take a look at some of these libraries in further detail.
As one of the oldest HTTP client libraries for Node.js, Axios stands out for its promise-based architecture, facilitating asynchronous communication seamlessly.
Below is a demo of how to implement the Axios library.
import axios from 'axios';
//const axios = require('axios'); // legacy way
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
Isomorphic: Enables code reusability across client and server environments.
Supports Cancellation: Allows premature termination of ongoing HTTP requests.
Interceptors: Empowers users to intercept requests for incorporating functionalities like authentication and error handling.
Automatic Serialization of Form Data: Simplifies sending form data in multipart/form-data format.
Despite its legacy status, many projects still rely on Request. However, it’s vital to note its deprecation. As a matter of best practice, it should no longer be used in applications.
Should you be faced with a situation where it’s required, below is a demo of how to implement Request.
const request = require('request');
request('http://www.google.com', function (error, response, body) {
console.error('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
**SSL/TLS Connection:**Request enables SSL/TLS connection, which is suitable for HTTPS requests. When an HTTPS URL request is made, Request leverages the Node.js in-built TLS module to establish a secure connection with the server. After the connection is established, the module initiates handshake with the server. This process involves verifying identities, exchanging certificates and agreeing on the encryption algorithm. After this, Request establishes a secure connection.
Multipart Form Data: Simplifies creation and transmission of multipart form data requests which are essential for file uploads or submission of forms with multiple fields.
Node-Fetch emerges as one of the most favored options, boasting simplicity and efficiency in implementation.
import fetch from 'node-fetch';
const response = await fetch('https://api.github.com/users/github');
const data = await response.json();
console.log(data);
Node.js Streams: Utilizes Node.js streams, making its API idiomatic for Node.js and compatible with all APIs.
Trusted http/https Modules: Leverages trusted http/https modules.
No Support for http/2: Lacks support for http/2 because http and https modules it uses for Node.js HTTP connection only support a limited amount of HTTP/2.
Not Standard-compliant: Despite the name, it does not implement the fetch specifications. Although this is better because it allows you to be compatible with Node.js streams API with many other things, it is not isomorphic.
Heavy Dependency Overhead: Reliance on http/https module may incur overhead costs. Moreover, Node-Fetch is now ESM only.
Prior to the arrival of Undici, Node.js relied on its core HTTP stack. However, significant improvements to this stack were challenging without risking disruptions to existing APIs, causing inconvenience for Node.js users and businesses.
This limitation stemmed from irregularities in the architecture of the former Node.js HTTP stack. Undici emerged as a solution to this problem, offering enhancements that were unattainable within the constraints of the core stack.
Today, Undici stands as a popular and modern HTTP client for Node.js applications, providing capabilities that surpass those of the traditional core stack.
Speed: Exhibits faster processing of HTTP requests compared to peers.
Stability: Demonstrates stability under heavy traffic conditions.
Easy to Understand: Offers comprehensive documentation for straightforward usage.
Below, we’ve benchmarked how Undici performs compared to the other options available within the Node.js ecosystem:

HTTP clients play a crucial role in server-side communications within Node.js applications.
In this blog, we delved into their functionality and explored popular client options, highlighting their respective advantages and limitations.
While exploring HTTP clients for Node.js, solutions like Undici address the limitations of traditional core stacks. However, for a comprehensive approach to streamlining Node.js backends, discovering tools like Platformatic is paramount.
Platformatic is designed to put Node.js backends on autopilot, simplifying development and deployment processes. Additionally, don't forget to explore the Platformatic client, which offers features such as creating a Fastify plugin to expose a client for remote OpenAPI or GraphQL APIs.