Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Empowering you to understand your world

How To Send A GET Request With Node.js (Using A Promise)

By Nicholas BrownFollow me on Twitter.

Writing an app frequently involves Internet connectivity, and ‘GET’ requests are often part of that. HTTP GET requests are a standard way of obtaining data from a server. This article shows you how to send a GET request from your Node.js app to another server.

An example (and common) use case of this is to download data such as weather data, exchange rates, or anything else from an API to incorporate into your own app (be sure to read the API’s terms and conditions!).

This article uses the ‘request-promise’ library to send the GET request.

Before proceeding, install the library with npm:

npm install request-promise

You can also try executing the request on a known domain such as kompulsa.com or Google and it will return the contents of the front page (primarily HTML).

Node.js GET Request Example

The sample code below will send a GET request to providerdomain.com/endpoint. ‘endpoint’ is the endpoint, which could be an API endpoint that provides data or it could just be a webpage. Store this code in a file called rptest.js.

const reqpromise = require('request-promise');

const options = {
    method: 'GET', //This specifies the type of HTTP request
    uri: 'https://providerdomain.com/endpoint', //Where the request is sent.
    json: true, //Specifying that the format is JSON.
    gzip: true
};

reqpromise(options).then(response => { //Execute the request
    console.log(response); //Print the server’s response to the screen. Executed if there is a response.
}).catch((err) => {
    console.log('API call error:', err.message);
});

console.log("Asynchronous test"); //Will be executed before the code above, because the code above is asynchronous

The function starting at ‘Execute the request’ is asynchronous, meaning that execution of your program will continue while it is waiting for the server to respond. This is useful because it won’t freeze your app while waiting for a response.

Bear in mind that if you need to prevent the code after this function from executing until a response has been received — you will need to rewrite it in a synchronous manner. To see it in action, run nodejs rptest.js and you should see something like this if it’s a web page (truncated):

$ nodejs rptest.js
Asynchronous test
<!doctype html >
<!--[if IE 8]> <html class="ie8" lang="en"> <![endif]-->
<!--[if IE 9]> <html class="ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en-GB"> <!--<![endif]-->
<head>

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published