We have already learnt to create parameterized request body by reading data from a CSV and JSON file. If you have not gone through them yet, you must go now.
We can store and use data from csv and json for validation or expected results as well. For this we must be able to read those data (variables) in scripts. We will going to learn the same in this post.
Postman Sandbox provides an API reference named “pm“. “pm.iterationData” API can be used to play around data variables. This
iterationData object contains data from the data file provided during a collection run.
You must be thinking that there will be multiple rows in csv or JSON Objects in JSON, so which row data it will return? It is a valid question. We know that mostly CSV or JSON data files are used to achieve data driven testing of API in Postman. Each row is considered as an iteration. So
iterationData Object always contains data from currently executing iteration row.
To get value of a data variable, we need to use get() method as below:-
Let’s add below codes in to Pre-request Script and Tests for API used in Creating parameterized request by reading data from a CSV . You can use example of JSON as well.
Let’s run above API using data file with one row and multiple rows :-
For data file with single row:-
For data file with multiple rows:-
Please note all above concepts are same for both CSV and JSON data files.