11 Workflows
title: "Workflows with setNextRequest" description: "Create conditional workflows in Postman collections using pm.setNextRequest for request chaining. Learn branching logic, iteration control, and workflow patterns for complex API testing scenarios." weight: 11 date: 2026-06-28 lastmod: 2026-06-28 tags: [api-development, postman] }
pm.setNextRequest controls the execution flow of Postman collection runs. It enables conditional branching, skipping requests, looping, and creating dynamic workflows. Without setNextRequest, requests execute in order.
What You'll Learn
- setNextRequest syntax and behavior
- Conditional branching based on response
- Looping and retry patterns
- Skipping requests dynamically
- Workflow termination
Why It Matters
setNextRequest transforms linear collection runs into dynamic workflows. You can retry failed requests, skip cleanup on errors, or loop until a condition is met.
Real-World Use
Payment API collections use setNextRequest to retry failed payments with different methods. Onboarding flows skip steps based on user type. Monitoring workflows loop health checks.
flowchart TD
Start[Request 1: Login] --> Success{Login Success?}
Success -->|Yes| Req2[Request 2: Get Users]
Success -->|No| Req3[Request 3: Register]
Req3 --> Req1[Request 1: Login]
Req2 --> Condition{More Pages?}
Condition -->|Yes| Req2
Condition -->|No| End[End]
Teacher Mindset
Use setNextRequest sparingly. Linear collections are easier to understand and debug. Use workflows when you need conditional logic that cannot be handled by data files or pre-request scripts.
Code Examples
// Example 1: Conditional branching
pm.test('Route based on response', () => {
const body = pm.response.json();
if (body.role === 'admin') {
console.log('User is admin, navigating to admin panel');
pm.setNextRequest('GET Admin Dashboard');
} else if (body.role === 'user') {
console.log('User is regular, navigating to profile');
pm.setNextRequest('GET User Profile');
} else {
console.log('Unknown role, ending workflow');
pm.setNextRequest(null); // Stop execution
}
});
// Example 2: Retry loop pattern
pm.test('Retry on failure', () => {
const maxRetries = 3;
const currentRetry = parseInt(pm.iterationData.get('retry') || '0');
if (pm.response.code !== 200) {
if (currentRetry < maxRetries) {
console.log(`Retry ${currentRetry + 1}/${maxRetries}`);
pm.iterationData.set('retry', currentRetry + 1);
// Delay before retry
setTimeout(() => {}, 1000);
pm.setNextRequest(pm.info.requestName); // Retry same request
} else {
console.log('Max retries reached');
}
} else {
pm.iterationData.set('retry', '0');
}
});
// Example 3: Pagination loop
pm.test('Loop through paginated results', () => {
const body = pm.response.json();
const currentPage = parseInt(pm.variables.get('current_page') || '1');
const totalPages = body.totalPages || 1;
// Extract items from current page
pm.variables.set('page_data', JSON.stringify(body.items));
if (currentPage < totalPages) {
pm.variables.set('current_page', currentPage + 1);
pm.variables.set('page', currentPage + 1);
console.log(`Fetching page ${currentPage + 1} of ${totalPages}`);
pm.setNextRequest('List Products');
} else {
console.log(`Completed pagination, total: ${body.total} items`);
pm.variables.set('current_page', '1');
}
});
Common Mistakes
- Creating infinite loops by forgetting to update the condition variable
- Using setNextRequest without understanding that it affects the entire collection run
- Not handling the case where the next request does not exist
- Overusing workflows instead of using data-driven testing
- Making workflows too complex to debug
Practice
- Create a workflow that routes to different requests based on user role.
- Implement a retry workflow that retries a request up to 3 times on failure.
- Create a pagination loop that fetches all pages of results.
- Add a workflow that skips cleanup requests if the main request failed.
- Challenge: Build a multi-branch workflow that tests different user flows based on login response.
FAQ
Mini Project
Build a Postman collection with a workflow: Login request that routes to Admin Dashboard (if admin) or User Profile (if regular user). Admin Dashboard has a pagination loop. Add a retry on the Login request (max 3 retries on 401). Skip cleanup on authentication failure.
What's Next
Next, you will learn about data-driven testing with CSV and JSON files.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro