Serve yourself combining Postman and Express

Mohammed Ali El Malhouf
YounitedTech
Published in
3 min readMay 29, 2020
Photo by Vincent Riszdorfer on Unsplash

Through this post we are going to explore one of the easiest ways to extend Postman functionalities by making your API testing easier and fun. The idea is running a basic Express server locally, that responds to Postman test scripts requests.

This idea allows you to:

  • Work around limitations, or missing features of Postman.
  • Avoid some repetitive tasks, thus saving time.
  • Have fun while testing APIs.

Some of the prerequisites to implement this, are :

  • Node.js
  • Some basic knowledge of JavaScript.

The setup

  1. Create a folder with any name you want. Navigate into it in the terminal, and run npm init, hit Enter each step of the way, to keep the default choices. A package.json should be created in your repository.
  2. In the same directory run : npm install express open this will install express , and another package we will use afterwards.

First example

To briefly provide the context of this example. While testing an API’s endpoint, we generate loan contracts PDF files encoded in Base64. While the number of use cases increases, so does these repetitive tasks :

  1. Copy the long Base64 text from postman
  2. Paste it in a text editor
  3. Use a plugin to decode it from Base64
  4. Save it in PDF format
  5. Open the file and verify the data

Assuming your API returns a response similar to :

{
"reference": "CFR2020ABX94",
"contract": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg...."
}
  1. Create a script.js file with the following content :

2. Run the commandnode script.js, which starts a local server listening on port:3000 with a POST /saveContractroute. This decodes the file from Base64 and saves it in PDF format on your local disk.

3. Go back to Postman, in your request’s test script. Send a request to the running local server using the pm.sendRequest()method like shown in the code snippet below :

Second example

This project’s entry point is a session initialized from an REST API endpoint. Basically the API returns a url embedding a JWT token. So each time I had to copy/paste the url and open it in the browser, which was a tedious repetitive task I had to get rid off. Plus, the numerous requests from colleagues, and the client-side asking me to generate those recurring urls for different scenarios.

The idea is simple, use the open package to open the url in the default browser. Supposing your API returns a payload that contains a redirectionUrlfield among other data, same as below :

{
.....
"redirectionUrl": "https://www.medium.com/@alee37",
.....
}

In the script.js created before, add a GET route that fetches the query parameter and opens the link in the default browser. Below a code snippet demonstrating how to :

Run the commandnode script.js in the Terminal to start the server 💡

Similarly to the first example, in the request’s test script call the created GET route :

As an agile tester, it is important to continuously improve practices for an efficient delivery. The demonstrated method saves me a lot of time and effort while testing APIs for critical projects.

Please note that the demonstrated examples are simple, it can be considered as a first pillar to automate much more tasks and help you in the future. Especially with a simple and intuitive framework like Express and Postman.

--

--