Do you really need 12 requests, or just two, or potentially one.
- Sale Request with Account Number
- Sale Request with Track Data
Are all the requests against the same end point with different data?
So it could just be…
The aim is to loop though your CSV but you sort of need to know which data points have to change within each request and which might be static through all requests to work out the best way to split things up, and whether it can be done with one request or really does need to be split up into 12 requests.
I’m wondering if it would be possible to have a single request single request with all of the fields that are static, and then use a pre-request script to retrieve the fields from the CSV and then add these to the request.
All of the data for each iteration is held within a special “data” dictionary.
If you just console log the data variable in your pre-request script, you should see an JavaScript object with all of the elements for that iteration.
console.log(data);
To update the body for an urlencoded request, it would be something like the following.
Considering the following basic request to Postman Echo.
In the pre-request script, you can add keys and values to the request.
let elementstoAdd = {"key":"password", "value":"123456"}
pm.request.body.urlencoded.add(elementstoAdd);
Which you can see were added into the request.
To loop though the data variable (which should be a single line of your CSV) and update the body, you can do the following…
for (let [key, value] of Object.entries(data)) {
console.log(key, value);
if (value) {
pm.request.body.urlencoded.add({"key": key, "value": value});
}
}
It will check if a particular column has a value, as I’m assuming one of the issues is that some of the lines in the CSV have data in certain columns and others don’t.
If you were sending the same elements on every request, then you probably don’t need any of this and should be able to just reference them directly as variables using {{data.columnname}} in your body.
The above is only really needed if certain requests have more or less elements that you have to cater for.