Chaining Requests From a CSV

Hello All,

I have a csv with a list of email,pin values. I want to use a collection run to iterate each row of the csv, use the email in a request that returns an account number, then use the account number and pin to update the account with a pin value.

I have a few questions:

Using a csv in a collection run will inherently iterate and run the requests individually per row, correct?
Whenever I use the data variables am I using the current value of say data.email and do not have to specify a position in an array?
When saving the account number in a local variable, will the current value of the variable be overwritten or will the new value be added to it? Meaning can I just use the variable name in each iteration or do I need to specify a position in the array?

Hey @chrismo16, welcome to the community! :wave:

When you’re using the collection runner, each row of your CSV data is converted to an item in the iteration data array.
So, for every iteration - it’ll give you the data of that row.
You can imagine it as: data.email is equivalent to csvData[currentIterationCount], but postman does that internally and you don’t have to worry about it.

So yes, using data.email or pm.iterationData.get(variableName) in your scripts to access the current data based on the iteration count.

1 Like

Thanks @singhsivcan for the reply. That was very helpful and addresses the first 2 questions.

The last question was regarding saving the account number received in the first response in a local variable and then using it in the second response. Will the variable be overwritten each time or will it become an array of values? Hopefully I’m describing that clearly.

It depends on how you are storing it, if you’re storing it in the form of an array then you can do that.
Otherwise, you can use a single value also.

What I mean by that is you keep pushing new values into an array on each iteration (and as per your iteration, the array indices will hold the value of each iteration):

let currentAccountNumbers = JSON.parse(pm.variables.get('currentAccountNumbers') || "[]");

currentAccountNumbers.push('SomeNewAccountNumber');

pm.variables.set('currentAccountNumbers', JSON.stringify(currentAccountNumbers));

Or if you can re-use the variable then (this will happen each iteration, but yes it’ll be overwritten):

let accNumber = 'SomeNewAccountNumber';

pm.variables.set('accNumber', accNumber);

Terrific. That was very helpful. Thank you again!

1 Like