Bird
Raised Fist0
Postmantesting~20 mins

Conditional request execution (setNextRequest) in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Postman Conditional Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the next request executed?
Given this Postman test script, which request will run next after the current one?
Postman
if (pm.response.code === 200) {
    postman.setNextRequest('RequestB');
} else {
    postman.setNextRequest('RequestC');
}
ANo next request will run; the collection stops
BRequestB if response code is 200, otherwise RequestC
CAlways RequestB regardless of response code
DRequestC if response code is 200, otherwise RequestB
Attempts:
2 left
💡 Hint
Check the condition inside the if statement and what setNextRequest is called with.
assertion
intermediate
2:00remaining
Which assertion ensures the next request runs only on success?
You want the next request to run only if the current request succeeded (status 200). Which assertion and setNextRequest code is correct?
Postman
pm.test('Status is 200', function () {
    pm.response.to.have.status(200);
    postman.setNextRequest('NextRequest');
});
AUse pm.test to check status 200 and call setNextRequest inside it
BCall setNextRequest before pm.test assertion
CCall setNextRequest unconditionally outside pm.test
DUse pm.expect(pm.response.code).to.equal(200) without setNextRequest
Attempts:
2 left
💡 Hint
Where should setNextRequest be placed to run only if assertion passes?
🔧 Debug
advanced
2:00remaining
Why does setNextRequest not work as expected?
This script aims to skip the next request if a condition is false, but the next request still runs. What is the issue?
Postman
if (pm.response.json().success === false) {
    postman.setNextRequest(null);
}
// Next request still runs
AsetNextRequest(null) is correct to stop execution; issue is elsewhere
BsetNextRequest(null) must be called inside pm.test block to work
CsetNextRequest(null) stops execution only if no other setNextRequest calls override it
DsetNextRequest(null) should be setNextRequest('null') as string
Attempts:
2 left
💡 Hint
Check if other scripts or requests call setNextRequest after this code.
🧠 Conceptual
advanced
2:00remaining
What happens if setNextRequest is not called?
In a Postman collection run, if a request script does not call postman.setNextRequest, what happens after this request finishes?
AAn error is thrown and the run aborts
BThe collection run stops immediately
CThe same request runs again in a loop
DThe collection runs the next request in the defined order
Attempts:
2 left
💡 Hint
Think about default behavior when setNextRequest is not used.
framework
expert
3:00remaining
How to conditionally loop a request using setNextRequest?
You want to repeat 'RequestA' until a response field 'done' is true. Which script correctly implements this loop?
Postman
const done = pm.response.json().done;
if (!done) {
    postman.setNextRequest('RequestA');
} else {
    postman.setNextRequest(null);
}
ALoop while done is false by setting next request to 'RequestA', else stop with null
BLoop while done is true by setting next request to 'RequestA', else stop with null
CAlways set next request to 'RequestA' regardless of done value
DUse postman.setNextRequest('null') as string to stop looping
Attempts:
2 left
💡 Hint
Check the condition logic and how setNextRequest controls looping.

Practice

(1/5)
1. What is the main purpose of pm.setNextRequest() in Postman?
easy
A. To decide which request runs next based on a condition
B. To send the current request multiple times automatically
C. To stop the entire collection run immediately
D. To reset all environment variables to default values

Solution

  1. Step 1: Understand the function of pm.setNextRequest()

    This function controls the flow of requests by specifying which request should run next.
  2. Step 2: Compare with other options

    Options B, C, and D describe different actions not related to controlling the next request.
  3. Final Answer:

    To decide which request runs next based on a condition -> Option A
  4. Quick Check:

    pm.setNextRequest() controls next request execution [OK]
Hint: Remember: setNextRequest controls the next request flow [OK]
Common Mistakes:
  • Thinking it repeats the current request
  • Confusing it with stopping the collection run
  • Assuming it resets variables
2. Which of the following is the correct syntax to set the next request named "Login" in a Postman test script?
easy
A. pm.setNextRequest(Login);
B. pm.setNextRequest("Login");
C. pm.setNextRequest('Login');
D. pm.setNextRequest(Login')

Solution

  1. Step 1: Check the syntax for string argument

    The request name must be passed as a string in quotes. Both single or double quotes are valid, but must be paired correctly.
  2. Step 2: Identify correct option

    pm.setNextRequest("Login"); uses double quotes correctly. pm.setNextRequest('Login'); also uses single quotes correctly. Both are valid syntaxes. pm.setNextRequest(Login); lacks quotes, causing a syntax error. pm.setNextRequest(Login') has mismatched quotes.
  3. Final Answer:

    pm.setNextRequest("Login"); -> Option B
  4. Quick Check:

    Request name must be a quoted string [OK]
Hint: Always quote request names in setNextRequest() [OK]
Common Mistakes:
  • Forgetting quotes around request name
  • Mismatched or missing quotes
  • Omitting semicolon at line end
3. Consider this test script in Postman after a request named "CheckStatus":
if (pm.response.code === 200) {
    pm.setNextRequest('ProcessData');
} else {
    pm.setNextRequest('ErrorHandler');
}
If the response code is 404, which request runs next?
medium
A. ErrorHandler
B. ProcessData
C. CheckStatus
D. No next request runs

Solution

  1. Step 1: Analyze the condition

    The script checks if response code is 200. If true, it sets next request to 'ProcessData'. Otherwise, it sets to 'ErrorHandler'.
  2. Step 2: Apply the response code 404

    Since 404 is not 200, the else block runs, setting next request to 'ErrorHandler'.
  3. Final Answer:

    ErrorHandler -> Option A
  4. Quick Check:

    404 ≠ 200, so next request = ErrorHandler [OK]
Hint: If condition false, else block sets next request [OK]
Common Mistakes:
  • Assuming 404 triggers the if block
  • Thinking current request repeats
  • Ignoring else block logic
4. You wrote this test script in Postman:
if (pm.response.code = 200) {
    pm.setNextRequest('NextStep');
} else {
    pm.setNextRequest('Stop');
}
What is the problem with this script?
medium
A. Request names should not be in quotes
B. Missing semicolon after pm.response.code
C. pm.setNextRequest cannot be used inside if statements
D. Using assignment (=) instead of comparison (===) in the if condition

Solution

  1. Step 1: Identify the if condition error

    The condition uses single equals (=), which assigns 200 to pm.response.code instead of comparing it.
  2. Step 2: Understand consequences

    This causes a bug: the if condition always evaluates to true, and response code is overwritten.
  3. Final Answer:

    Using assignment (=) instead of comparison (===) in the if condition -> Option D
  4. Quick Check:

    Use === for comparison, not = [OK]
Hint: Use === for comparison, not = assignment [OK]
Common Mistakes:
  • Confusing = with === in conditions
  • Removing quotes from request names
  • Thinking setNextRequest can't be conditional
5. You want to create a Postman flow where if a variable userExists is true, the next request is "GetUserData", else the flow stops. Which script correctly implements this in the test tab?
hard
A. if (pm.variables.get('userExists') === true) { pm.setNextRequest('GetUserData'); } else { pm.setNextRequest(null); }
B. if (pm.variables.get('userExists') == 'true') { pm.setNextRequest('GetUserData'); } else { pm.setNextRequest('Stop'); }
C. if (pm.variables.get('userExists')) { pm.setNextRequest('GetUserData'); } else { pm.setNextRequest(null); }
D. if (pm.variables.get('userExists') === 'true') { pm.setNextRequest('GetUserData'); } else { pm.setNextRequest(null); }

Solution

  1. Step 1: Understand variable type and condition

    pm.variables.get returns a string or boolean depending on how set. Using it directly in if checks truthiness correctly.
  2. Step 2: Check stopping the flow

    Setting pm.setNextRequest(null) stops the collection run, which matches the requirement to stop if userExists is false.
  3. Step 3: Evaluate options

    if (pm.variables.get('userExists')) { pm.setNextRequest('GetUserData'); } else { pm.setNextRequest(null); } uses direct truthiness check and null to stop, which is correct. Options A and C compare strictly to boolean or string which may fail if variable type differs. if (pm.variables.get('userExists') == 'true') { pm.setNextRequest('GetUserData'); } else { pm.setNextRequest('Stop'); } tries to set next request to 'Stop' which is not a request name and won't stop the flow.
  4. Final Answer:

    if (pm.variables.get('userExists')) { pm.setNextRequest('GetUserData'); } else { pm.setNextRequest(null); } -> Option C
  5. Quick Check:

    Use truthy check and null to stop flow [OK]
Hint: Use null in setNextRequest to stop flow [OK]
Common Mistakes:
  • Comparing variable to string 'true' instead of boolean
  • Setting next request to a non-existent name to stop
  • Not using null to stop the collection run