Typically when you start a collection run, Postman runs all requests in the same order they appear in your collection. Requests in folders are executed first, followed by any requests in the root of the collection.
In the Collection Runner, you have the option to change the order of the requests before starting a run. However, instead of manually changing the request order each time you run the collection, you can automate this behavior using the pm.execution.setNextRequest()
function.
As the name suggests, pm.execution.setNextRequest()
enables you to specify which request Postman runs next, following the current request. Using this function, you can build custom workflows that chain requests, running them one after the other in a specific order.
To specify the request to run next, add the following code on the Post-response tab of a request. Replace request_name
with the name of the request you want to run next.
pm.execution.setNextRequest("request_name");
Postman runs the specified request after completing the current request.
If you pass the name of the current request to the setNextRequest
function, Postman will run the current request repeatedly.
Important! Make sure to wrap
setNextRequest
in some extra logic so the request doesn't loop indefinitely. For example, you might exit the loop after a certain number of iterations or when another condition is met. Otherwise you will need to force close the Collection Runner to end the loop.
To stop a workflow, add the following code on the Post-response tab of a request:
pm.execution.setNextRequest(null);
The collection run will stop after Postman completes the current request.
You can also skip the execution of a request from the Pre-request tab using the
pm.execution.skipRequest
method. Learn more about skipping request execution from pre-request scripts.
Keep the following tips in mind when using the pm.execution.setNextRequest()
function.
The pm.execution.setNextRequest()
function is intended for use when running a collection using the Collection Runner, the Postman CLI, or Newman. It has no effect when you run a request using Send.
You can use pm.execution.setNextRequest()
in the pre-request script or the post-response script of a request. If more than one value is assigned, the last value that's set takes precedence.
Instead of specifying the name of the request to run next, you can provide the request ID. To find the request ID, open a request and select the information icon in the right sidebar. You can also get the request ID using the pm.info.requestId
function (see Scripting Workflows).
The pm.execution.setNextRequest()
function is always executed at the end of the current request. If you put other code blocks anywhere in the pre-request script or post-response script after this function, the code blocks will still execute before pm.execution.setNextRequest()
.
The scope of pm.execution.setNextRequest()
is the source of your collection run.
pm.execution.setNextRequest()
is limited to that folder. In this case, you can set any request in the same folder as the next request, but not requests located in other folders or at the root of the collection.Learn more about running collections or folders.
After learning about how to build request workflows, you can write some scripts.
Last modified: 2023/10/05
Additional resources
Videos
Blog posts
Case Studies