5 Ways to Make HTTP Requests in Node.js

Making HTTP requests is a core functionality for modern languages and one of the first things many developers learn when acclimating to new environments. When it comes to Node.js there are a fair amount of solutions to this problem both built into the language and by the community. Let’s take a look at some of the most popular ones.

HTTP – the Standard Library

First on our hit parade is the default HTTP module in the standard library. With this module, you can just plug and go without having to install external dependencies. The downside is that it isn’t very user friendly compared to other solutions.

The following code will send a GET request to NASA’s API and print out the URL for the astronomy picture of the day as well as an explanation:

consthttps=require('https');https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY',(resp)=>{letdata='';// A chunk of data has been recieved.resp.on('data',(chunk)=>{data+=chunk;});// The whole response has been received. Print out the result.resp.on('end',()=>{console.log(JSON.parse(data).explanation);});}).on("error",(err)=>{console.log("Error: "+err.message);});

Much of the HTTP, and the HTTPS, module’s functionality is fairly low-level. You’re required to receive response data in chunks rather than just providing a callback function to be executed as soon as all of the data is received. You also need to parse the response data manually. This is fairly trivial if it is JSON formatted, but it is still an extra step.

One other problem is that this module does not support HTTPS by default, so we need to require the https module instead if the API we are using communicates over HTTPS.
It may take a bit more effort to get the data you want, but is a great utility if you don’t want to add too many dependencies to your codebase or want access to its low level functionality.

Request

Request is a simplified HTTP client comparable to Python’s requests library. This library is much more user friendly than the default http module and has been considered a go-to for the community for several years.

This has been my personal choice since I’ve started using Node.js, and is great for quickly getting things done. Unlike the http module, you will have to install this one as a dependency from npm.

Run the following in your terminal from the directory you want your code to live in:

npminstallrequest@2.81.0

You’ll see that you need much less code to accomplish the same task that we did above:

Asynchronous code can easily become over complicated and unpleasant to deal with, and the way Axios tackles this problem may make your life easier in the long run.

SuperAgent

Similarly to Axios, SuperAgent is another popular library primarily used for making AJAX requests in the browser but works in Node.js as well. Install SuperAgent with the following command:

npminstallsuperagent@3.5.2

What is cool about SuperAgent is that you have other useful functions that you can chain onto requests such as query() to add parameters to the request. We’ve been just manually adding them in the URL in the previous examples, but notice how SuperAgent gives you a function to do this:

Got is great if you want a smaller library that feels less “bloated” than something like Request.

Final Thoughts

This doesn’t cover all of the solutions, but now you see how the basic functionality works in a few of the most popular HTTP libraries in Node. There are also libraries such as node-fetch which ports the browser’s fetch functionality to the backend.

Other languages have a similar variety of libraries to tackle this problem. Check out these other tutorials in Swift, Python and Ruby. Also, check out our Node.js Quickstarts for a place to apply your new skills.

What are your favorite ways to send HTTP requests? Feel free to reach out and let me know or ask any questions: