pm.execution.setNextRequest does not work

This is my post-response script.

But when I run collection;

I get this response. My setNextRequest Function does not work.

Hey @ibrahimgunes :wave:

Welcome to the Postman Community! :postman:

Without more context and information, it’s not clear what is happening on your side.

Can you update the question and provide more details, please.

1 Like

Hi Danny thank you for feedback. I updated my question. Can you check again? Thank you for your help.

There isn’t a request called ‘errorRequest’ in your Collection.

What are you trying to reference there?

If you comment out that line then save the request, is it then going to the next one in the Collection?

Status code is 200. But nextResponse(‘comments’) does not work

What are you trying to accomplish here with setNextRequest inside some nested if statements.

What’s the workflow you’re trying to create?

If you removed all of that script, the collection runner would move to the next request on it’s own.

I’m not understanding why you need to check if the event is a pre-request, when you’ve added that to the post-response section - Then check it’s it the post-response section and then check the status code.

Without images it’s hard to know exactly what you have in place now, you need to add screenshots and add more details to your replies.


This is my post-response. I want to basicly When I send a request posts, I want it to also send a request to comments. I want to just use setNextRequest

Based on the sequence of the requests within that collection, when you use the Collection Runner (setNextRequest only works with that) it would always go from your posts request to your comments request.

The setNextRequest function isn’t required there, unless you either need to stop the execution of that iteration or you want to send the request flow sequence down a different path.

I’m still unsure why you require any of the original code except for the 200 status code test.

If you want to send 2 requests in the same main request, you would want to be looking at using sendRequest() in the scripts section, to send an async request.

Actually I want to do autamtion test. For example I want to send 1000 request with different user ID. Like That;

for(i=0; i<1000;i++){
//change dynamic user ID and send request again
}

Search the forum for example of using sendRequest with the array.shift() method.

setNextRequest only sets the next request will run after the current request and all of the code in the pre-request and post-response tabs.

If you have a for loop, it will just quickly loop through the 1000 items and will only use setRequest with the last item in that loop.

The idea is that you create an array with your 1000 user ID’s in it. Once you have that, you need an IF statement checking the size of the array. If the array is larger that zero, then you use array.shift() to get the first element from the array (it will also delete that element from the array) and use setNextRequest to keep looping on the same request. You can save that first element to an collection or environment variable that can be used in the request.

If you want to run two requests in your loop, then you would have the IF statement in the post-response code for your second request. If you want to loop on a single request, you can control this all in the pre-request script of the request.

I would suggest testing this with 10 elements before moving onto larger numbers.

Here is a simple example…

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", userid + count);

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

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.