Making HTTP requests with Node.js

TABLE OF CONTENTS

Perform a GET Request

There are many ways to perform an HTTP GET request in Node.js, depending on the abstraction level you want to use.

The simplest way to perform an HTTP request using Node.js is to use the Axios library:

JS
const axios = require('axios');
axios
.get('https://example.com/todos')
.then(res => {
console.log(`statusCode: ${res.status}`);
console.log(res);
})
.catch(error => {
console.error(error);
});

However, Axios requires the use of a 3rd party library.

A GET request is possible just using the Node.js standard modules, although it's more verbose than the option above:

JS
const https = require('https');
const options = {
hostname: 'example.com',
port: 443,
path: '/todos',
method: 'GET',
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', d => {
process.stdout.write(d);
});
});
req.on('error', error => {
console.error(error);
});
req.end();

Perform a POST Request

Similar to making an HTTP GET request, you can use the Axios library library to perform a POST request:

JS
const axios = require('axios');
axios
.post('https://whatever.com/todos', {
todo: 'Buy the milk',
})
.then(res => {
console.log(`statusCode: ${res.status}`);
console.log(res);
})
.catch(error => {
console.error(error);
});

Or alternatively, use Node.js standard modules:

JS
const https = require('https');
const data = JSON.stringify({
todo: 'Buy the milk',
});
const options = {
hostname: 'whatever.com',
port: 443,
path: '/todos',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
},
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', d => {
process.stdout.write(d);
});
});
req.on('error', error => {
console.error(error);
});
req.write(data);
req.end();

PUT and DELETE

PUT and DELETE requests use the same POST request format - you just need to change the options.method value to the appropriate method.

    Contributors
  • flaviocopes
  •  MylesBorins
  •  fhemberger
  •  LaRuaNa
  •  ahmadawais