If else condition for Postman Requests

Hi guys, need help from you guys for a usecase

I have a scenario where session expires in 20mins and i need to refresh the session after expiry.

Use case : If any request fails with 401 , i need to call a new request which will refresh the session.

If Response code = 401 for Request1 ,
ignore tests and, Call Request2 and go back to Request 1 :
else Call Request 3

Can someone help me with script for the usecase

1 Like

Hi @swasthik! It looks like you’re going to want to control the request workflow using the postman.setNextRequest method. This is a general idea of some logic you can put it the “Tests” tab of your first request:

if(pm.response.code == "401"){
    postman.setNextRequest("req2");
} else {
    postman.setNextRequest("req3");
    // Insert logic here for other tests
}

By using conditional logic like if statements, you can control which requests run in order if they’re run from the Collection Runner.

If you want a bit more info on controlling your workflow, here’s a piece that helped me a lot when I first started :slight_smile:

2 Likes