Repeatedly call an API using parameters based on pm.response.headers values

tests

#1

Hello,

I have a database table with approx 4,000 records (currently). A response to an API call (POST, JSON) gives me data of this table for a maximum of 1,000 records per API call. A parameter ‘PageNo’ defines which of the 4,000 records are selected (e.g. PageNo = 1 gives me record 1-1000). The header data of the response includes a ‘PageCount’, in my example 4. I am able to retrieve that ‘PageCount’ and the test below loops through the PageNo (result in Postman Console = 1 2 3 4).

How I can call the same request repeatedly in a loop and use the values of the PageNo (i) as a parameter for that request like so:

{{baseUrl}}/v1/Units/Search?PageNo={{i}}

In my example I would expect the request to run 4 times with PageNo2 = 1, 2, 3, 4.

I am aware that I can use a CSV file and loop through the request in Collection Runner but PageCount changes (i.e. the number of records in the table change) and I need to run this loop frequently so creating a new CSV file for each loop is not really an option.

Postman Test:

pm.environment.set('Headers2', JSON.stringify(pm.response.headers));
var Headers2 = JSON.stringify(pm.response.headers);

pm.environment.set('PageCount2', JSON.parse(Headers2)[10].value);
var i;
for (i = 1; [i] <= pm.environment.get('PageCount2'); i++) {
    console.log(i);
    postman.setNextRequest('custom fields | json Copy');
    
}

Postman Request:

    {
      "Location":"{{TestingLocation}}",
      "Fields":[
      	"StockNo",
      	"BrandDesc"
      	],
      "Filters": {
          "StatusCode":"{{TestingUnitSearchStatusCode}}"
      },
      "PageSize":1000,
      "PageNo" : "{{i}}"
    }

Thank you very much for your help.


#2

It’s the i value that you want to store in an environment variable rather than the headers as you want to remember how many pages have been so far.

So, make sure you add an i variable to your environment and set it’s value to 1. And be sure to pick this environment when using the collection runner.

You should also retrieve the header value in a safer way - the header you’re interested in might not always be at index 10.

As a note on your syntax - you should also remove the [] around the i in your for loop.

When you tidy that up, it would leave the test looking like:

var i = Number(pm.environment.get('i'));
var pageCount = pm.response.headers.find(header => header.key === 'PageCount').value;

if (i < pageCount) {
    pm.environment.set('i', i + 1);
    postman.setNextRequest('custom fields | json Copy');
}

#3

Thanks very much Matt, that solved my problem. :grinning:

I am now able to loop through the pages and get my data in this format:

[{“StockNo”:“0001”,“BrandDesc”:“Brand1”}, [{“StockNo”:“0002”,“BrandDes”:"Brand2”}]

These are two records out of 4,000 and they are all returned in one line. Is there a way to add line breaks to the API response? I also have the option to choose txt, xmll or csv format which would remove the [{]} characters but again no line breaks.

Ideally I would like to get the response in the following format:

StockNo, BrandDesc
0001, Brand1
0002, , Brand2

Thank you for your help.