Open In App

Postman Branching and Looping

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Postman’s Collection Runner offers a streamlined way to execute requests within a collection. By default, requests follow the order they appear in the collection. However, with the postman.setNextRequest() function, users can automate and modify the sequence in which requests are executed, allowing for the creation of custom workflows and precise control over request chaining.

Understanding postman.setNextRequest()

postman.setNextRequest() is a function in Postman’s scripting environment that determines the next request to be executed after the current one finishes. It enables users to build tailored workflows by specifying which request should follow the current request.

Setting the Next Request

To define the subsequent request in a workflow, insert the following code snippet into the Tests tab of a request:

pm1

postman.setNextRequest(“request_name”);

Replace “request_name” with the name or request ID of the intended next request. I have added request_name as “Branching and Looping”. Once the current request is completed, Postman will automatically proceed to execute the specified request.

Looping Over a Request

Bypassing the name or ID of the current request to this function, Postman will repeatedly execute the current request until specified otherwise.

However, it’s crucial to implement additional logic within the script to prevent indefinite looping. Consider adding conditions to exit the loop after a certain number of iterations or upon meeting specific criteria.

postman-conditional-looping

Postman Conditional Looping

Stopping a Workflow

To halt the workflow after the completion of the current request, use the following code snippet within the Tests tab:

postman.setNextRequest(null);

Implementing this line instructs Postman to stop executing subsequent requests, effectively ending the collection run.

Best Practices and Considerations

1. Usage in Pre-request or Test Scripts

postman.setNextRequest() can be employed in either the pre-request or test scripts of a request. However, if multiple values are assigned, the last value set will take precedence.

2. Identifying Request IDs

  • Request IDs can be used in place of request names when setting the next request.
  • These IDs can be found by opening a request and accessing the information icon in the right sidebar or using the pm.info.requestId function.

3. Scope and Execution

  • The function is executed at the end of the current request, irrespective of other code blocks within pre-request or test scripts.
  • Its scope is confined to the source of the collection run.
  • Running an entire collection allows setting any request within the collection as the next request.
  • When running a folder, the scope is limited to requests within that folder only.

Conclusion

In conclusion, postman.setNextRequest() empowers users to orchestrate and customize request execution sequences within Postman. Its functionality extends to creating intricate workflows, implementing conditional branching, and ensuring precise control over the flow of requests.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads