My end goal is to be able to call a particular request multiple times within one workflow.
Say for example I want create a set of users such that I can call more advanced requests on them.
I need to perform an initial setup (ONLY ONCE) in my collection run and a final clean up (ONLY ONCE).
My initial thought was to do it with pre-request script on the collection but turns out that that code is executed at for every request (which makes absolutely no sense to me of that design choice, if someone could explain).
So I am trying to see if I can include a same request multiple times in a collection but that is also not an option (which again makes me think that either I am missing out on something important conceptually or I really don’t understand why that design choice )
I wasn’t able to follow your use-case ! Would you be able to provide more context? Screenshots, sample code etc wold help. Also not sure if you are taking about Postman Flows? Do clarify.
pre-request as the name signifies, is the script that runs before every request, and is used to programatically control aspects of the request about to be sent. You can use pm.info object to get iteration information, when running your collection using runner.
Example script that you can put in your Collection level pre-request:
if(pm.info.iteration === 0) {
// Run code that you want to run once!
console.log('Setup')
}
And in your last request’s test script add the following to clean-up
if(pm.info.iteration === (pm.info.iterationCount - 1)) {
// Your clean-up code goes here
console.log('Clean-up')
}
You can put the clean-up code at Collection level too, inside “Test”, but you would then need to check if the current request is the last one
if(pm.info.iteration === (pm.info.iterationCount - 1)
&& pm.info.requestName === "<Your Last Request's Name>") {
// Your clean-up code goes here
console.log('Clean-up')
}
Without seeing more details on the flow you are trying to achieve, I would recommend that your setup and tear down requests are proper requests.
You can use sendRequest() in a pre-request script, but I don’t think that will be needed in this scenario (and in my opinion it makes it harder to read and troubleshoot). It’s much better if you can run this using full requests.
You then want to loop on a request (or set of requests) that are in the middle of the collection.
This is done using setNextRequest() and a useful JavaScript function called array.shift().
Please note, setNextRequest() only works with the collection runner.
If you had 3 requests that you wanted to loop through.
You would store the array of users in a collection or environment variable.
Please use JSON.stringify() when storing arrays, and JSON.parse() when retrieving.
The first request would have a pre-request script that retrieves the array of test data\users.
Using array.shift(), this will retrieve the first value from the array, and then delete it from the array.
You will then save the current value to a new variable that can be used in the request.
You will also re-save the array collection variable (which will now have one less record).
In the tests tab for the 3rd request (or in the same request if its only one request). You will retrieve the array again. At the end of the tests tab, you will have an IF statement that checks the size of the array, and if its more than > zero, you will use setNextRequest() to run the first request in the set of 3 again.
If the size of array is zero, then it should then move onto the tear down script.
Just a bit of advice on tear down scripts. It’s better to check for the known state in the startup script, than rely on the tear down script from a previous run to have completed.