Oops! Something went wrong while submitting the form.
Ways to make HTTP requests in Node.js
Anom Warbhuvan
~ 10 min read | Published on May 24, 2023
As you know Node.js is used to build server-side applications using Javascript, and even supports HTTP requests. This makes it a powerful platform for building web applications that rely heavily on server-side communication and data transfer.
Making HTTP requests in Node.js is an important action item for most developers. And inspite of it being simple on the surface, there is more that one must know about them, specially the different ways for making HTTP requests.
So today, let’s talk about how different packages like Axios, Got, Node-Fetch, Request-Promise, Simple-Get, SuperAgent, and HTTPS module, can be used to make HTTP requests.
To understand our program better we will test it by making GET requests to a mock API using all possible HTTP client options. This way we will be able to ensure that the program is functioning correctly and that it can handle different types of requests and configurations. Refer to this mock API:
Before we dive in, it’s good to get ready with the following prerequisites you’ll need to try the shared code.
Install a Node.js runnable environment on your machine. Make sure you have latest versions of Node.js and npm installed.
The npm commands are used to install the required package which in return makes our development process easier with specific requests over a server. You can use npm init command to initialize node package and npm install --save <module-name> to initialize it, for your project.
If you want to run Javascript files, use node <file_name> on your compound line and get the desired output in your code editor console.
HTTP(S) - Node.js Standard library
Getting started with HTTP(S) module, is absolutely hassle free. It is a pre-installed module that comes with Node.js bundle. It is used by many Node.js projects by default, including popular frameworks like Express and Koa. often use it as a reference point for comparing and evaluating other HTTP request libraries for Node.js.
To understand more about HTTP(S)-Node.js Standard Library, let’s look at this mock Zipy API code snippet to send a GET request:
// Import the 'https' module
const https = require('https');
// Set the options for the HTTPS request, including the hostname, path, method, and any query parameters or headers
const options = {
hostname: 'api.zipy.ai',
path: '/services/errors?api_key=DEMO_KEY',
method: 'GET'
};
// Send the HTTPS request with the specified options
const req = https.request(options, res => {
// When the response is received, log the status code to the console
console.log(`statusCode: ${res.statusCode}`);
// Listen for the 'data' event and write the response data to the console
res.on('data', d => {
process.stdout.write(d);
});
});
// If an error occurs, log it to the console
req.on('error', error => {
console.error(error);
});
// End the request
req.end();
Note that in a real-world scenario, you would want to replace DEMO_KEY with your own API key if you have one, and handle any errors that may occur during the request
Here’s a breakdown of how the above code works:
Imports the built-in HTTP(S) module in Node.js.
Creates an options object that contains the necessary information to make the HTTP request. It specifies the hostname of the server, the path of the API endpoint, and the HTTP method to use.
This will create an HTTP request using the 'https.request()' method and passes in the options object created earlier. It also defines a callback function to handle the response received from the server.
The request() method of these modules is asynchronous, meaning that it does not block the main thread of execution while the request is being made.
We can also handle errors that may occur during the HTTPS request and also end the request. The 'req.end()' method sends the request to the server.
Axios
Axios is a promise based HTTP client which blends with browser as well as node.js environment. Axios includes tools which can automatically convert request and response data into JSON. It is easier to send asynchronous HTTP requests to REST API endpoints and perform CRUD operations on it. Axios supports HTTP/2 when used on client side.
As of April 2023, the Axios github repository had over 85,000 stars and 9,300 forks, indicating a sizable and active user and contributor community. Some well-known Node.js projects, such as the vue and nuxt frameworks, the redux state management library, and the gatsby static site generator, use Axios.
Install Axios using npm command:
npm install axios@latest
Accomplish your process with Axios:
// Import the 'axios' library
const axios = require('axios');
// Make an HTTP GET request to the API endpoint with the specified URL
axios.get('https://api.zipy.ai/services/errors?api_key=DEMO_KEY')
.then(response => {
// If the request is successful, log the status code and response data to the console
console.log(response.status);
console.log(response.data);
})
.catch(error => {
// If an error occurs, log it to the console
console.error(error);
});
Note that in a real-world scenario, you would want to replace DEMO_KEY with your own API key if you have one, and handle any errors that may occur during the request
Here’s a breakdown of how the above code works:
We use the get method of the axios library to make an HTTP GET request to the endpoint 'https://api.zipy.ai/services/errors', passing the query parameter 'api_key' with a value of 'DEMO_KEY'.
Once the response is received from the server, the code logs the HTTP status code of the response to the console using response.status, and logs the response data to the console using 'response.data'.
If an error occurs during the HTTP request, the catch block logs the error to the console using 'console.error()'.
Overall, this code makes a GET request to a mock Zipy API to fetch information about errors, using a demo API key.
Got
Got library is a popular HTTP request library for Node.js which helps developers send HTTP requests and handle responses. Its simplified API promises usability and error handling, makes it easy to use. It provides a slimmed-down version of the requests package. It also has a promise-based API, HTTP/2 support, and a pagination API, which are unique to the Got library. The GitHub repository for Got library has gained 17,000 stars and 1,200 forks, as of April 2023.
Install Got using npm command:
npm install got@latest
Here’s the code for using Got:
// Import the 'got' library
const got = require('got');
// Make an HTTP GET request to the API endpoint with the specified URL, query parameters, and response type
got('https://api.zipy.ai/services/errors', {
searchParams: {
api_key: 'DEMO_KEY'
},
responseType: 'json'
})
.then(response => {
// If the request is successful, log the status code and response body to the console
console.log(response.statusCode);
console.log(response.body);
})
.catch(error => {
// If an error occurs, log it to the console
console.error(error);
});
Again, note that in a real-world scenario, you would want to replace DEMO_KEY with your own API key if you have one, and handle any errors that may occur during the request.
Here’s a breakdown of how the above code works:
Import the "got" library and assign it to a variable named got.
Use the "got" library to send a HTTP GET request to the '/services/errors' endpoint of the Zipy API. The options for this request are provided as an object with the following properties: - searchParams: It contains the query string parameters for the HTTP request. - responseType: It is used for specifying the expected response type for the HTTP request. The response is expected to be in JSON format.
.then() method calls the returned promise object from the "got" request. It will take a callback function that will be executed if the request is successful. The callback function takes one argument, which is the HTTP response object. Inside the callback function, the response's status code and body are logged to the console using the console.log() method.
.catch() method is called on the promise object as well. It takes a callback function which gets executed when request encounters an error. Only an error object is taken an argument. The error gets logged to the console using the 'console.error()' method inside the callback function.
Node-Fetch
Node-Fetch library is a light wrapper HTTP request library which brings the browser window.fetch method to Node.js. It provides a consistent and easy-to-use API for making HTTP requests in Node.js, with a focus on native promises, and async/await syntax along with window.fetch.
Metrics for Node-Fetch suggest it as a widely used and popular HTTP request library, as it has 15,000 stars and 1,000 forks, as of April 2023.
Install Node-Fetch using npm command:
npm install node-fetch@latest
Check out how node-fetch can be used to call mock Zipy API:
// Import the 'node-fetch' library
const fetch = require('node-fetch');
// Make an HTTP GET request to the API endpoint with the specified URL and query parameters
fetch('https://api.zipy.ai/services/errors?api_key=DEMO_KEY')
.then(response => {
// If the request is successful, log the status code to the console and return the response data as JSON
console.log(response.status);
return response.json();
})
.then(data => {
// If the response data is successfully parsed as JSON, log it to the console
console.log(data);
})
.catch(error => {
// If an error occurs, log it to the console
console.error(error);
});
Note that in a real-world scenario, you would want to replace DEMO_KEY with your own API key if you have one, and handle any errors that may occur during the request.
Here’s a breakdown of how the above code works:
Import the 'node-fetch' library and assign it to a variable named 'fetch'. Send an HTTP GET request to the URL using it.
Handle the HTTP response information by calling .then() method on the promise. There are two “.then()” method, the first one takes a function as a parameter which gets executed when the promise is resolved. The second one takes the 'response' object as a parameter, which contains the HTTP response information.
The 'response.status' property contains a status code which is printed as console.log() function.
To parse the response body as JSON, apply '.json()' method on the response object.
The second .then() method is called on the promise returned by '.json()' to handle the parsed JSON data. The method takes a function as a parameter that will be executed when the promise is resolved. The function takes the parsed JSON data as a parameter.
The 'console.log()' method is used to log the parsed JSON data to the console.
.catch() method is called on the promise to handle any errors that may occur during HTTP request or parsing of the response body. Finally, we log the error object to the console using 'console.error()' method.
Request-Promise
Request-Promise library allows HTTP requests in Node.js to be made using promises. It is powered by Bluebird. You will see, how it adds Bluebird powered .then(…) method to the request call objects. With a moderately sized community of users and contributors, the Request-Promise 1,900 stars and 220 forks, as of April 2023.
Install Request-Promise using npm command:
npm install request-promise@latest
Below snippet will send a GET request to the mock Zipy API:
// Import the 'request-promise' library
const rp = require('request-promise');
// Define options for the request, including the API endpoint URL, query parameters, and expected response type
const options = {
uri: 'https://api.zipy.ai/services/errors',
qs: {
api_key: 'DEMO_KEY'
},
json: true
};
// Make an HTTP GET request to the API endpoint with the specified options
rp(options)
.then(response => {
// If the request is successful, log the response data to the console
console.log(response);
})
.catch(error => {
// If an error occurs, log it to the console
console.error(error);
});
Note that in a real-world scenario, you would want to replace DEMO_KEY with your own API key if you have one, and handle any errors that may occur during the request.
Here’s a breakdown of how the above code works:
Just like all other modules, the code starts with an import of the library and assigns it to a variable named rp.
The options object is defined, which contains the configuration options for the HTTPS request. The uri property specifies the URL that the request should be sent to, and the qs property is an object that contains the query string parameters to be sent with the request. In this case, the api_key parameter is set to 'DEMO_KEY'. Set the json property to 'true', which specifies that the response should be parsed as JSON.
After that, the rp function sends a HTTPS GET request to the URL specified in the options object. It will return a promise that resolves the HTTP response information.
Now, we call .then() method on the promise to handle the HTTP response information.
Here, console.log() method is used to log the response object to the console. It will then parse the JSON response data, since the json property of the options object was set to true.
Now, you can call the .catch() method on the promise to handle any errors that may occur and log the error object to the console.
Simple-Get
Simple-Get library is a lightweight HTTP request library for Node.js that makes it easy to send HTTP requests and handle responses. It is a library which is more efficient and has less code. Similar to other libraries, Simple-Get allows redirects and composes well with npm packages for features like cookies, proxies, form data, & OAuth.
Compared to the above libraries, Simple-Get library is relatively less popular with 240 stars and 29 forks on its github repository.
Install Simple-Get using this npm command:
npm install simple-get@latest
Below code will explain how Simple-Get accomplish the request:
// Import the 'simple-get' library
const simpleGet = require('simple-get');
// Define the URL of the API endpoint
const url = 'https://api.zipy.ai/services/errors?api_key=DEMO_KEY';
// Make an HTTP GET request to the API endpoint with the specified URL
simpleGet(url, function (err, response) {
// If an error occurs, log it to the console and return
if (err) {
console.error(err);
return;
}
// Log the status code of the response to the console
console.log(response.statusCode);
// Pipe the response data to the standard output stream
response.pipe(process.stdout);
});
Note that you should replace DEMO_KEY with your own API key if you have one, and handle any errors that may occur during the request in a real-world scenario.
Also, keep in mind that this code snippet may need to be modified depending on the specifics of the API you're calling and the data you're expecting in the response.
Here’s a breakdown of how the above code works:
The code imports the "simple-get" library and assigns it to a variable named 'simpleGet'.
Defines a variable named 'url' including the 'api_key' parameter with a value of 'DEMO_KEY'.
Sends HTTP GET request to the URL
Here, the callback function takes two parameters, 'err' and 'response'. If an error occurs during the HTTP request, 'err' will contain the error information. If the request is successful, "response" will contain the HTTP response information.
If err is not null, then there has been an error. If the error is null, then the request was successful, and the console.log() method logs the status code of the answer.
'response.pipe()' method is called on the response object to pipe the response to the standard output stream. Then, the response is logged to the console.
SuperAgent
The SuperAgent package is a well-known HTTP request library for Node.js that makes it simple to send HTTP requests and manage results. It is similar to Axios which is primarily used for making AJAX requests in the browser as well as in Node.js. SuperAgent, lets you chain onto requests such as query() to add parameters to the requests. Earlier, developers had to manually add them in the URL.
SuperAgent is used on a large scale and has a active community of users and contributors scaling its Github repository to 13,000 stars with 1,200 forks in the latest report of April 2023.
Install SuperAgent using npm command:
npm install superagent@latest
Run through this code snippet to understand how SuperAgent functions:
// Import the 'superagent' library
const request = require('superagent');
// Define the URL of the API endpoint
const url = 'https://api.zipy.ai/services/errors';
// Make an HTTP GET request to the API endpoint with the specified URL
request
.get(url)
.query({ api_key: 'DEMO_KEY' })
.end((err, res) => {
// If an error occurs, log it to the console and return
if (err) {
console.error(err);
return;
}
// Log the response body to the console
console.log(res.body);
});
Note that you should replace DEMO_KEY with your own API key if you have one, and handle any errors that may occur during the request in a real-world scenario.
Here’s a breakdown of how the above code works:
Import 'Superagent' library and assign it to a variable named request. Now define a variable named url that contains the URL of the Zipy API's '/services/errors' endpoint.
The request object is used to send an HTTP GET request to the 'url' variable value, which is the “/services/errors” endpoint of the Zipy.ai API. The request query string parameters are set using the .query() method, which takes an object containing the 'api_key' parameter with a value of 'DEMO_KEY'. The 'end()' method is then used to send the request and take a callback function as a parameter.
The callback function takes two parameters, err and res. If an error occurs during the HTTP request, first parameter will contain the error information. If the request is successful, second parameter will contain the HTTP response information.
If an error is not null, the code reports it to the console using the 'console.error()' method, and if it is null, it is logged to the console using the console.log() function in the body of the response.
Most popular library to make HTTP request
To find out which Node.js HTTP client library is winning the most hearts, we went to npm trends, and checked the downloads in the last one year for Axios, Got, Node-Fetch. Request-Promise, Simple-Get, and SuperAgent. If you are wondering why did we skipped HTPPS standard Node library, the answer is - it is already a part of the Node.js package and gets downloaded with the Node bundle.
It is crystal clear in terms of downloads, Node-Fetch is the most popular, and Request-Promise is least popular. Node-Fetch has made HTTPS requests more efficient and fast, which rightfully earns the spot. All the HTTP requests libraries give a great deal of freedom in how requests and responses are handled.
Conclusion
Node.js offers several ways to make HTTP requests to web servers and API’s, each with its own advantages and use cases. All the above libraries mainly do the same thing - much like changing the quantity of ingredients in the same recipe.
Ultimately, choosing one method over the other will mostly depend on the specific requirements of the project at hand, like ease of use, performance, security, and compatibility with existing code. We hope this list helped you understand the different methods for making HTTP requests In Node.js and pick the one that’s right for your project.
Happy Coding!
Call to Action Feel free to comment or write to us in case you have any further questions at support@zipy.ai. We would be happy to help you. In case you want to explore Zipy for your app, you can sign up here or book a demo here.
Call to Action Feel free to comment or write to us in case you have any further questions at support@zipy.ai. We would be happy to help you. In case you want to explore Zipy for your app, you can sign up here or book a demo here.
Call to Action Feel free to comment or write to us in case you have any further questions at support@zipy.ai. We would be happy to help you. In case you want to explore Zipy for your app, you can sign up here or book a demo here.
Call to Action Feel free to comment or write to us in case you have any further questions at support@zipy.ai. We would be happy to help you. In case you want to explore Zipy for your app, you can sign up here or book a demo here.
Call to Action Feel free to comment or write to us in case you have any further questions at support@zipy.ai. We would be happy to help you. In case you want to explore Zipy for your app, you can sign up here or book a demo here.
Call to Action
Feel free to comment or write to us in case you have any further questions at support@zipy.ai. We would be happy to help you. In case you want to explore for your app, you can sign up or book a demo.