Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Consume Remote API Data From Within A Node.js Application

TwitterFacebookRedditLinkedInHacker News

Not too long I wrote about consuming remote API data using Golang. However, as you know, I’m also a heavy Node.js developer as well. So how do we issue HTTP requests from within a Node.js application and why might you want to. The simple answer is that you might want to consume someone else’s data within your web application and you can’t just do that like you would in a browser-based application using Ajax or similar.

We’re going to see how to make HTTP requests possible within Node.js.

To save us the trouble of first building a remote web service, we’re going to make use of a very popular and very easy to use web service offered by httpbin. With this service we can issue requests and essentially get what we’ve sent, back as a response. While it is not the funnest example, it is a functional example.

The first step is to include the appropriate Node.js package in our project. Using the Node Package Manager (NPM), execute the following:

npm install request --save

The above command will download the request package and save it to our package.json file.

Let’s say that we want issue a GET request and determine the IP address of the request, in this case our local machine. Within your project include the following:

var Request = require("request");

Request.get("http://httpbin.org/ip", (error, response, body) => {
    if(error) {
        return console.dir(error);
    }
    console.dir(JSON.parse(body));
});

The above snippet will issue a GET request and print out an error if it exists. Otherwise, if no error exists in the response, the response body will be parsed into a JavaScript object and then printed in the console.

Issuing GET requests is great, but what if you needed your application to write or change the remote data using a POST request or similar?

Let’s change our code to look like the following:

var Request = require("request");

Request.post({
    "headers": { "content-type": "application/json" },
    "url": "http://httpbin.org/post",
    "body": JSON.stringify({
        "firstname": "Nic",
        "lastname": "Raboy"
    })
}, (error, response, body) => {
    if(error) {
        return console.dir(error);
    }
    console.dir(JSON.parse(body));
});

Notice in the above snippet that we’ve changed our code to .post instead of .get. Most, not all, APIs expect requests to come in with a JSON body. For this reason we need to specify the body type as header information. The body cannot be sent as a JavaScript object so it must first be serialized into a string. Because we are explaining the format via a header, the receiving server will be able to parse the string back into an object.

Just like with the GET request we can check for an error and process the response body.

Conclusion

You just saw that it is not difficult to issue HTTP requests from within your Node.js application. This is very different from receiving requests from clients as this time we’re interested in consuming data from a remote API rather than receiving it or returning it.

If you’re interested in creating your own API that can receive HTTP requests and distribute data, check out my previous tutorial titled, Create A Simple RESTful API With Node.js.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.