javascriptcsvpostman

Run several requests with different number of data from csv on Postman


I have one .csv file with some data for my Postman like this:

Name,City
Jhon,New York
Lucy,London
Adam,
Alex,

and i have one collection with 2 reqs:

 - GetAge
{
{{Name}}
}


- GetInfoCity
{
{{City}}
}

How can i run my collection with .csv file to run first 4 times req1 (GetAge) and then 2 times req2 (GetInfoCity), using a data for my .csv file?

Thanks


Solution

  • I'll post the same answer that I put on the Postman forum.

    As you will always have the name, you don’t need to do anything particular with the first request. You just let it run normally.

    You can use the skipRequest() function to skip the City request if the data is missing from the CSV file.

    let currentCity = pm.iterationData.get("City");
    
    if (currentCity === undefined || currentCity === null || currentCity === '') {
        console.log(`${pm.info.requestName} : No City - skipping request`)
        pm.execution.skipRequest();
    
    } else {
        pm.environment.set("City", currentCity);
        console.log(`${pm.info.requestName} : ${pm.environment.get("City")}`)
    }