Testing APIs with Postman

Postman is a nifty application with a simple and intuitive interface for developing and testing APIs; it let developers simulate HTTP requests and store them for use later. Additionally, Postman analyzes the responses sent by the API and visually displays them for each request(s) made. The tool considerably reduces the time required to develop and test your application.

The application is prevalent in the developer community with 5 million developers are using it, and more than 100,000 companies use it to access 130,000,000 APIs every month.

Installation πŸ“¦

There are two ways to install the application:

  1. Install it locally on your computer, and
  2. Install it as a Chrome Extension

You can download and installation packages on the Postman website by selecting your operating system (Windows, Linux or Mac).

If you install the application successfully, Postman’s default screen will look like this:

Postman

The process of simulating requests is not complicated either. Choose the type of requisition (GET, POST, PUT, PATCH, e.t.c.) you want to perform, as shown below:

Postman

Enter the URL in the address bar and use the blue send button to make an API request.

If the request has parameters, it is possible to inform the URL, or click with the Params button and enter the values ​​in the key and value fields, for example:

url? param1 = value1 & param2 = value2.

Below are some of the functions:

Postman

Postman

Note that if you enter something other than JSON format , it already indicates that there are syntax problems.

Postman

Create a Node.js API and Test with Postman πŸ”¨

Now that you know what Postman is and its functions, it is time for us to create a Node.js API and test it with Postman.

mkdir nodejs-api && cd nodejs-api && npm init

The content of your package.json should look similar to what’s below:

{
  "name": "nodejs-api",
  "version": "0.0.1",
  "description": "Test Postman with a Node.js API.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "api",
    "postman",
    "node.js"
  ],
  "author": "Finbarrs Oketunji <oketunjifinbarrs@gmail.com>",
  "license": "ISC"
}

Create index.js and code the application:

touch index.js

Create a route for the application:

mkdir routes && cd routes && touch index.js && touch routes.js

The new project structure will look something like this:

nodejs-api
β”œβ”€β”€ routes
    β”œβ”€β”€ index.js
    β”œβ”€β”€ routes.js
β”œβ”€β”€ index.js
└── package.json

Note that we specified port 3000 inside index.js to start listening to HTTP requests and put a console to display in the terminal when the server is online. Also, we are calling the routes folder that is inside the nodejs-api folder. Let’s create her files next.

The content of our index.js is below:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const port = 8000;

require('./routes')(app, {});
app.listen(port, () => {
  console.log('Application is live @ ' + port);
});

In the index.js file inside the routes folder, let’s add the followings:

const textRoutes = require('./text_routes');

module.exports = function(app, db) {
  textRoutes(app, db);
};

Since we are calling the file routes.js in index.js, we have to add what’s below to the file:

module.exports = function(app, db) {

  app.post('/text', (req, res) => {
    res.send('We are rocking with Postman!')
  });
};

In the code above, when the application receives a POST request for the route/text, it will execute the code that is within the callback passing a request object (req) and a response object (res). The answer we are going to send is the phrase β€œWe are rocking with Postman!”.

Let us run and test our application with Postman. First, we need to install the dependencies with:

sudo npm i

Type npm start in the terminal to start the application:

npm start

Postman

In Postman, you can send a POST request to localhost:3000/text and then click on SEND.

Postman

The status of the request is 200 which means it was successful, and our request returned the phrase β€œWe are rocking with Postman!” that we specified in the application.

Another exciting feature of Postman is how easy it is to create a Mock Server. A mock server has the function of simulating requests without having to develop an API in Node.js as we did above. If you want to know more about how it works, send me an e-mail, and I will create a Mock Server article.

The Node.js API we created in this article is on Github. Addtionally, you can learn more by reading the Postman Documentation.

Don’t stop learning!

😎πŸ”₯πŸš€