https://www.twilio.com/blog/2017/08/http-requests-in-node-js.html


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.

We’ll be using NASA’s Astronomy Picture of the Day API as the JSON API that we are interacting with in all of these examples because space is the coolest thing ever.

giphy.gif
Before moving on, make sure you have up to date versions of Node.js and npm installed on your machine.

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:

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:

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

Request is a fantastic option if you just want an easy to use library that deals with HTTP requests in a sane way. If you want to use Promises, you can check out the request-promise library.

Axios

Axios is a Promise based HTTP client for the browser as well as node.js. Using Promises is a great advantage when dealing with code that requires a more complicated chain of events. Writing asynchronous code can get confusing, and Promises are one of several solutions to this problem. They are even useful in other languages such as Swift.

To install Axios from npm, enter the following command in your terminal:

The following code will accomplish the same task of logging the URL to and of explaining the astronomy picture of the day:

Axios even parses JSON responses by default. Pretty convenient! You can also see that error handling is done with .catch() since we are using promises now.

You can even make multiple concurrent requests with axios.all if you wanted to do something like get the astronomy picture of two different days at once:

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:

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:

Just like with Axios you don’t have to parse the JSON response yourself, which is pretty cool.

Got

Got is another choice if you want a more lightweight library. It is also available to use in Twilio Functions.

Again, install Got with npm:

Similarly to Axios, Got works with Promises as well. The following code will work as the rest of the examples do:

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 SwiftPython 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:

Build More With JavaScript

SMS and MMS Notifications with Node.js
Getting started with Web Components building a Video Chat widget

How to Validate Phone numbers in Node/JavaScript with the Twilio Lookup API

Get started with writing TypeScript today!