Keep trying API until a match is found

I am calling an API to get a list of phone numbers by area codes. I have a pre-request-script that generates a random area code. The problem is that some of these randomly generated area codes are not always valid area codes. The API always returns a 200 ok either with a list of area codes or a message that says the “No phone numbers for this area code”. I would like the pre-script to generate a new area code if the response is incorrect until a valid area code is sent. I have tried several ways of doing this and gotten nowhere. Any suggestions?

function getRandomAreaCode() {

  let rand_phone = pm.variables.replaceIn('{{$randomPhoneNumber}}');

  // Returns only the first three numbers
  return rand_phone.substring(0,3);
}

areaCode = getRandomAreaCode();

// This gives me an typeerror
//pm.expect(pm.response.text()).to.include("No phone numbers");

//console.log("Getting next iteration");
//postman.setNextRequest();

//This does not work either
//if (typeof response !== "undefined") {
  //pm.collectionVariables.set("area_code", areaCode);
  //console.log("Did not find a match");
//}

pm.collectionVariables.set("area_code", areaCode);

The elegant (and correct) way to do this, is by being in control of your test data, so instead of creating random and/or invalid area codes, try creating an array with all your test data.

You can this from a CSV file or do something like this

let areaCodes = pm.collectionVariables.get("locAreaCodes");

// Create array
if(!areaCodes || areaCodes.length == 0) {
    areaCodes = [{"123"},{"456"},{"7890"}];
}

// Remove and return first element of array
let currCode = areaCodes.shift();

// update array for next iteration
pm.collectionVariables.set("locAreaCodes", areaCodes);

If you really want to use randomly generated data, I would check the response and use setNextRequest

var jsonResp = JSON.parse(responseBody);
pm.expect(jsonResp).to.have.property("areaCode");

Or

pm.environment.set("myVar", jsonResp.data.areacode);

Then handle myVar


// If var is undefined run the request again
if (myVvar) {
   postman.setNextRequest('current step name')
}

If you need to kill the test set postman.setNextRequest(null)

1 Like

I agree with the part about being in control of your test data.

Not sure if the array shift method is needed, as I don’t think the poster wants to loop through all of the test data, but just needs a valid area code for the request.

Postman has the lodash library built in, so the following example is one way of doing this.

However, as this is just JavaScript, there are quite a few wants to achieve the same thing.

let areaCodes = [123, 456, 789]; // or read this from a collection variable
let randomAreaCode = _.sample(areaCodes);
console.log(randomAreaCode);

Remember to stringify arrays when storing them as collection variables, and JSON.parse them when retrieving.

I would include as many example area codes as possible. If you only include 3 or 4, it might not appear to be that random.