Sometimes you want to run different requests based on results from earlier requests. Conditional request execution helps you control which request runs next.
0
0
Conditional request execution (setNextRequest) in Postman
Introduction
You want to skip a request if a previous test failed.
You want to run a specific request only if a value in the response meets a condition.
You want to loop back to a request until a condition is met.
You want to stop running requests after a certain condition.
You want to create dynamic workflows in your API tests.
Syntax
Postman
pm.setNextRequest('Request Name');Use the exact name of the request as it appears in your Postman collection.
Calling pm.setNextRequest(null); stops the collection run.
Examples
This will make Postman run the request named 'Get User Details' next.
Postman
pm.setNextRequest('Get User Details');If the response status is 200, run 'Process Data' next; otherwise, stop running requests.
Postman
if (pm.response.code === 200) { pm.setNextRequest('Process Data'); } else { pm.setNextRequest(null); }
This loops the 'Repeat Request' up to 3 times, then stops.
Postman
let count = pm.environment.get('count') || 0; if (count < 3) { pm.environment.set('count', count + 1); pm.setNextRequest('Repeat Request'); } else { pm.setNextRequest(null); }
Sample Program
This script runs after the 'Login' request. If login is successful (status 200), it runs 'Get Profile' next. Otherwise, it stops the collection run.
Postman
// Test script for 'Login' request if (pm.response.code === 200) { pm.setNextRequest('Get Profile'); } else { pm.setNextRequest(null); }
OutputSuccess
Important Notes
Make sure request names are spelled exactly as in your collection.
Using pm.setNextRequest(null); stops the collection run immediately.
Conditional execution helps create smarter, efficient test flows.
Summary
Use pm.setNextRequest() to control which request runs next.
It helps you skip, repeat, or stop requests based on conditions.
This makes your API tests more flexible and realistic.