setNextRequest() after skipRequest() in pre-script does not go to that request, but stops execution

Under certain conditions I want to skip current request and continue with other requests.
That other request might be the same request (with other parameters made in the pre-script) or another request.

When the condition fires in the pre-request-script, I do:
pm.execution.skipRequest();
pm.setNextRequest(nextrequestname);

I verified that variable nextRequestname indeed does work; without the skiprequest it will fire (after this request)
I verified that this request was not fired with that skipRequest();

so, it seems that in the pre-request-script the setNextRequest is ignored when skipRequest() is used too.

Is it possible to somehow not execute the request, but still trigger the post-response script? That could be an alternative to setNextRequest in the pre-request.

Hey @ariew :wave:

Welcome to the Postman Community! :postman:

It not that it’s specifically ignoring .setNextRequest, it’s ignoring all scripts.

Any remaining scripts in the Pre-request tab are skipped, and no tests are run.

As the setNextRequest function would be in the Post-Response script, it’s not going to run.

In order to get what you’re trying to do, the logic would need to be in that previous request in order to move to the next intended request to execute.

Thank you indeed.

You said script stops running after the .SkipRequest. That is not true. Script code after the .SkipRequest does run, but obviously the post-test script does not work (just as I want it to be skipped).
However, I tried the .SetNextRequest from the posttest and that is working.
Trying the .SetNextRequest from the pre-request code will not work, because ‘this’ test cannot be skipped.

I want in the pre-request code select a specific test to run, without running the current test. This is not possible yet, at least not with the combination .SkipRequest and .SetNextRequest.

It depends where you have placed the pm.execution.skipRequest() statement in the pre-request script. If you have some logic, tests, console logs or whatever else before that statement it will run that in the Script because it wouldn’t know that you want to skip the request yet. If it was on the first line of the pre-request it wouldn’t execute anything after it.

For example, that would hit the first console log statement but not the one after the skipRequest statement.

As you can only use pm.setNextRequest() in the post-response script, it wouldn’t run that because the request wouldn’t have been sent so there would be no response and the post-response script wouldn’t be executed.

This wouldn’t execute any of this post-response script

I don’t have any idea what your script looks like so I wouldn’t be able to tell if what you have runs after that skipRequest statement or not.

Just a reminded that the setNextRequest() function will only work in the Collection Runner and has no effect if you’re trying to see that working when sending a single request.


I want in the pre-request code select a specific test to run, without running the current test.

I don’t really understand what you mean here. Are you mixing up Tests with Requests?

It’s probably just me not understanding what you’re trying to do with the current functionality.

I have lots of script examples for looping with pm.execution.setNextRequest controlling the loop from the pre-request script.

It just sets the request that will run next after the current request and all of the code in the pre-request and post-response scripts.

The following is an example script that loops the same request x number of times.

x = 10;

if (typeof array === 'undefined' || array.length == 0) {
    array = Array.from({length: x}, (_, i) => i + 1)
    pm.environment.unset("currentCount");
}

let count = array.shift();
pm.environment.set("currentCount", count);

if (array.length > 0) {  
    currentRequest = pm.info.requestName;
    pm.execution.setNextRequest(currentRequest);
} 

This all runs from the pre-request script with nothing in the post-response script.

1 Like

Are you also able to provide a method that will help the original question?

I haven’t tested it yet, but I would try putting the setNextRequest() within the pre-request script before the skipRequest() function.

1 Like

This appears to work.

I have three requests in a folder.

The requests are simple GET requests to Postman Echo with a query parameter that matches the request name.

I have a pre-request script on request1.

pm.execution.setNextRequest("request3");
pm.execution.skipRequest();

Running this in the Collection Runner, the Console log shows that only request 3 was actually run.