简体   繁体   中英

Postman: iterating through a list in a Pre-request Script

I've got the following in a postman pre-request script. Currently I have to have one POST for each userlocation, and each POST defines a separate "searchLoc", so the list below would require 4 posts (my original list is 200 locations). I'm wondering if there is a way to iterate through the list with just one POST. Maybe a folder based pre-request script, but can't figure out how to either walk the list, or repeat the same POST 200 times.

var userLocations = [["WDC","Washington",38.8976,-76.0476],
["MEX","Mexico City",19.7665,-100.1692],
["MIA","Miami",25.8456,-80.2082],
["SFO","San Francisco",37.8236,-122.4217]];

var searchLoc = "DC";
for ( i=0; i<userLocations.length; i++) {
  if (!userLocations[i].indexOf(searchLoc)) break;
}
var username = userLocations[i][0]+"_"+userLocations[i][1];
var lat = userLocations[i][2];
var lng = userLocations[i][3];

postman.setEnvironmentVariable("username", username);
postman.setEnvironmentVariable("lat", lat);
postman.setEnvironmentVariable("long", lng);

I think you can use Newman and write a bash script. In script you can iterate through the list and pass locations to the postman global variables and in the same loop just run the collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM