Introduction:
Request is a small library that implements fully and true streaming XHR for browsers that support these methods. It uses a variety of proprietary response Type properties to force a streaming connection, even for binary data. This module comes with built-in protection against ActiveX blocking that is frequently used in firewalls & virus scanners.
API testing using Request with JavaScript is an easy framework for testing API’s. Let’s perform a basic CRUD operation with request module offered by npm.
Pre-requisite:
- Latest version of Node JS.
- Latest version of npm
Installation
The node modules should be installed in your system that will help to run test cases written in JavaScript. Just create a directory and initialize the npm package to your root directory. Use the command below.
npm init
Then install Request API in your package to make HTTP requests. Use the code below.
npm install request
You need to install a testing framework to perform API testing using Request API . You can use either Mocha, Jasmine or Jest. Assuming you install Mocha as testing framework by the below code.
npm i mocha
By defining test scripts as mocha in package.json writes your API test cases for GET, PUT, POST, DELETE, PATCH etc…
You need to install a Chai assertions library to get responses to HTTP requests. Use the command below.
npm i chai
Syntax
Use request to make HTTP responses. A simple syntax for using request is below:

Create [POST]:
Creating data using the Request in API testing requires a request module offered by npm. To get response we use chai assertions library. You can post an API request using bearer Token and the data needs to create.

Read [GET]
In a similar way to POST method GET method also requires library from Request API and chai assertion library. The framework design pattern uses mocha as a testing framework. The code for GET request is below.

Update [PUT]
For update, data using API request from PUT method follows a similar process as the POST method. The code for the PUT request is below.
By getting status code as 200. That means your data is updated successfully.

Delete [DELETE]
To DELETE the data using a delete request and get response code as 204. The code for the DELETE request is below.

Pros
- For finding bugs at an early stage of software development, API testing using the Request JS module is an efficient tool for fast and reliable performance.
- Can be used with different testing frameworks like Chai and Mocha.
- We can easily stream the response to the file stream.
- It provides custom HTTP headers.
Cons
- The API is currently depreciated and for that reason, no future updates are possible.
- The community support for this API is low in comparison to other testing tools.