I am trying exercise an API that returns only 10 results on the first request and sets a property to use to request more. My first attempt is simply getting all the pages at once by putting the following code in the “Test” section of Postman:
let echoPostRequest = {
url: '{{Zuora-Sandbox-URL}}/v1/catalog/products',
method: 'GET',
header: 'Authorization: Bearer {{currentAccessToken}}',
};
var response = pm.response.json();
let shouldContinue = response.nextPage ? true : false;
let pageCount = 0;
const requestFunction = (err, res) => {
var responseJson = res.json();
console.log(responseJson.nextPage);
console.log(echoPostRequest.url);
if(res.code == 200 && responseJson.nextPage) {
shouldContinue = true;
echoPostRequest.url = pm.environment.get('Zuora-Sandbox-URL') + responseJson.nextPage;
pageCount++;
return;
}
shouldContinue = false;
};
// Imaginge all your tests are here.
// If the value of pageURL field is NOT an empty string, hit the endpoint again instead of advancing to the next request in the collecito.
while(shouldContinue) {
pm.sendRequest(echoPostRequest, requestFunction);
}
But this causes Postman to “hang”. Viewing the console just results in a spinning cursor for what seems forever. So I have two questions. Why doesn’t the above work? and What would be the best way to get a certain page or retrieve all the pages?