Loop Through Array in Postman

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

Let 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). And as in my previous article I chose Google Books API for that:

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

In Postman UI you need to create:

Globals: initial and current value must be 0
Initial and current value must be 0

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

  • Specify an array of items;
  • Get an index for current request from variable;
  • Set an item according to index into another variable for 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]);
Pre-request Script tab: «varVolume» variable will create automatically
«varVolume» variable will create automatically

In the Request on «Tests» tab you need:

  • Increment index and save a new value after current request. It will be used for request a new item on the next iteration;
  • Case 1: Output target data from response into 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 conditionif (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    pm.globals.set("resposeData", `${previousResponse}"${jsonData.id}": "${jsonData.accessInfo.viewability}"`);}
Tests tab: «responseData» variable will create automatically
«responseData» variable will create automatically

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

  • Set a number of iterations = quantity of your data items;
  • Open Postman’s console.
Collection Runner: iterations = array.length
Iterations = array.length
Collection Runner: example of the run
Example of the run

Open Postman’s console and select «Hide network» . You’ll see all your target data in 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).

Manage Environments: data stores as the variable value in copy-pastable format
Data stores as the variable value in 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.

--

--

--

Quality assurance engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My Top React Interview Questions

Summer Wallpaper Aesthetic Laptop

Writing unit-testable JavaScript code

The Essential Guide To Mastering JavaScript Functions

Web layout skeleton with pure CSS

Implementing a local microservice using Node.js and Serverless

JavaScript block bindings and es6 every developer must know!

Selenium 4 FAQs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrey Enin

Andrey Enin

Quality assurance engineer

More from Medium

End-to-End testing with Cypress — Part 1

Generate Mochawesome HTML / JSON Reports in Cypress

Cypress for Test Automation from Scratch

Ways to skip test cases in Cypress.io