Loop Through Array in Postman

Sometimes, as a test engineer, you need to iterate over a set of elements and call the same API method with each of them.

Andrey Enin
3 min readFeb 28, 2020

If you do not have ready testing infrastructure, you can use Postman Collection Runner for a quick solution.

Let’s assume that you have an array (or any collection) of items, each of which you need to pass into the same request.

As an example, I decided to find which of 10 books from Google Books has a preview (it will be an array of items). As in my previous article, I chose Google Books API for that:

https://www.googleapis.com/books/v1/volumes/{{varVolume}}?key={{yourAPIKey}}

In the Postman desktop application, you need to create:

The initial and current values must be 0
Fig. 1. The initial and current values must be 0

In the Request on the «Pre-request Script» tab, you need:

  1. Specify an array of items;
  2. Get an index for the current request from the variable;
  3. Set an item according to the index into another variable for the current request.
const dataArray = [
"TWiYDwAAQBAJ",
"8ahgioXQ9g8C",
"K98hhw0IEHgC",
"mDKphT8_XLsC",
"XfDOcmJisn0C",
"vxX2JGsN7PoC",
"5x4uDwAAQBAJ",
"mmlgAAAAMAAJ",
"jetfAAAAMAAJ",
"38xQHS4h0yEC"
];

let item = pm.globals.get("itemOfArray");

pm.globals.set("varVolume", dataArray[item]);
{varVolume} variable will be created automatically
Fig. 2. {varVolume} variable will be created automatically

In the Request on «Tests» tab, you need:

  • Increment index and save a new value after current request. It will be used to request a new item on the next iteration;
  • Case 1: Output target data from the response into the console;
  • Case 2 (advanced): Save target data into a variable.
let item = pm.globals.get("itemOfArray");
pm.globals.set("itemOfArray", Number(item) + 1);

let jsonData = pm.response.json();
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

// Output only in case of condition
if (jsonData.accessInfo.viewability !== "NO_PAGES") {
// Case of output №1 - in Postman console
console.log(`${jsonData.id} is ${jsonData.accessInfo.viewability}`);

// Case of output №2 - in Postman variable
// Additional variable to avoid "undefined" previous data on the first iteration
let previousResponse = (pm.globals.get("resposeData") === undefined) ? '' : `${pm.globals.get("resposeData")}, `;

// Represent output as a key:value data
const keyInQuotes = `"${jsonData.id}"`;
const valueInQuotes = `"${jsonData.accessInfo.viewability}"`;
pm.globals.set("resposeData", `${previousResponse}${keyInQuotes}: ${valueInQuotes}`);
}
{responseData} variable will be created automatically
Fig. 3. {responseData} variable will be created automatically

Now run your single request in Collection Runner. Before starting, you need:

  1. Set a number of iterations = quantity of your data items;

2. And open Postman’s console.

Iterations = array.length
Fig. 4. Iterations = array.length
Example of the run
Fig. 5. Example of the run

Open Postman’s console and select «Hide network». You’ll see all your target data in a defined format (output case 1).

Console: hide network
Hide network in console log

Open Postman’s environments manager and check the value of the variable with response data (output case 2).

Data stores as the variable value in a copy-pastable format
Fig. 7. Data stores as the variable value in a copy-pastable format

After the current run you should reset the iterable variable to its default value (0) and delete temporary created variables to prevent any failures at the next run.

--

--

Andrey Enin

Quality assurance engineer: I’m testing web applications, APIs and do automation testing.