Hello,
I’ve been trying to get pm.execution.setNextRequest to work as documented, but so far I haven’t been successful—even when using the example provided in your own documentation:
To isolate the issue, I created a minimal Collection that sends a simple GET request to localhost:3001. My goal is to loop through a list of usernames stored in a collection variable. The first request is sent successfully, but the Collection does not continue looping. Instead, it halts after the first iteration with an error that provides no detail.
Here is the setup:
Pre-request script:
let usernames = pm.collectionVariables.get("usernames");
if(!usernames || usernames.length == 0) {
usernames = ["AsdBsd", "asd123", 123456, "123asd", "asdfghjklzxcvbnmqwertyuio"];
}
let currentUsername = usernames.shift();
console.log('currentUsername: ', currentUsername)
pm.collectionVariables.set("username", currentUsername);
pm.collectionVariables.set("usernames", usernames);
post-response script
const usernames = pm.collectionVariables.get("usernames");
if (usernames && usernames.length > 0){
console.log('setNextRequest: ', usernames)
pm.execution.setNextRequest(pm.info.requestId);
} else {
console.log('setNextRequest: null')
pm.execution.setNextRequest(null);
}
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Result:
- The first request runs and logs as expected.
- After that, execution stops with an error bubble that gives no information (screenshot below).
- This happens consistently, regardless of environment or browser.
Logs
Error bubble without definition
At this point, I’m trying to determine whether this is a bug, a deprecated feature, or if there’s some undocumented limitation or setup requirement. I’d appreciate any help or clarification on what might be going wrong here.
Thank you..
All of this can be done in a pre-request script.
Here is a working example…
Request body using query parameters
Pre-request script
if (typeof usernames === 'undefined' || usernames.length == 0) {
// new run - setup usernames array
// global variable which will persist through the collection run
// does not need to be saved as a collection variable
usernames = ["User1", "User2", "User3"];
// new run - clear down existing collection variables
pm.collectionVariables.unset("username");
}
let currentUser = usernames.shift(); // retrieve first object from array, then delete it from the array
// set collection values from current object
// this does need to be set as a collection variable if you want to use it in the body or query parameters
pm.collectionVariables.set("username", currentUser);
// are the collection variables set correctly?
console.log(pm.collectionVariables.get("username"));
// loop while contentType array is more than zero.
if (usernames.length > 0) {
currentRequest = pm.info.requestName;
pm.execution.setNextRequest(currentRequest);
}
You shouldn’t need to use setNextRequest(null) unless you specifically want to end the current iteration. This should just stop executing when the array gets to zero size.
Corresponding Console Log
Mike, thank you for your suggestion. Attached is a screen recording of my attempt at getting this to work. It still does not work. Am I running it correctly? I am running it as part of a Collection. Postman-echo endpoint, pre-request script only. What am I missing?
Is there an environment setting somewhere that needs to be turned on? Or is this a limitation with the VS Code Extension.
(attachments)
The setNextRequest function\method requires the Collection Runner to work.
I don’t know if the runner within the VS code extension is supported. I know that Newman is and I would have thought that VS code would have this functionality.
Hopefully someone from Postman will be along with more insight.
Can you try saving the collection and then trying to run it from the web gui? You should have access to the same collection through the GUI, so it should be a relatively quick test to narrow down if the issue appears to be with the VS code method. You shouldn’t have to make any changes, you should be able to just run it from the GUI.