How to automate API testing using protractor

Making HTTP requests for APIS with NodeJS is a means for fetching data from a remote source. It could be an API, a website, or something else: at one point you will need some code to get meaningful data from one of those remote sources.

We can use browser or postman for performing the API tests manually but when it comes to the automation we have to use some libraries to automate the APIs.

During your career as a Web Developer / Tester you will work with HTTP requests most of the time. Let's learn some different ways of making HTTP requests in NodeJS.

Below are few libraries which will help us to automate the requests, these libraries are useless if you are not using proper frameworks like Jasmine, or protractor. These libraries will work without frameworks as well.

  • http/https
  • Request
  • Axios
  • node-fetch
  • r2
  • Needle
  • SuperAgent

request npm module to test API

Protractor http request

HTTP requests are a core part of the most of the modern languages. It can be challenging for new developers to learn how to make HTTP requests to exchange data.

Luckily, for NodeJS developers, it is not the case. There are many battle-tested solutions available for making any kind of HTTP request.

Let's take a look at some of the most popular NodeJS libraries that support HTTP requests.

With http and https 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. For the testing purpose, we will be using Jsonplaceholder api.

const https = require('http');

https.get('', (response) => {
  let todo = '';

  // called when a data chunk is received.
  response.on('data', (chunk) => {
    todo += chunk;
  // called when the complete response is received.
  response.on('end', () => {
}).on("error", (error) => {
  console.log("Error: " + error.message);

Nested try..catch..finally

Post method

const https = require('https')

const data = JSON.stringify({
  todo: 'Buy the milk'

const options = {
  hostname: '',
  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) => {

req.on('error', (error) => {


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions