Implementing Conditional Requests in Postman
Let’s consider an example where we want to create a new user only if the user doesn’t exist; otherwise, we update the existing user data.
1. Create Requests:
For this scenario we would require creating three requests in Postman:
Request 1 (GET): Fetches user data.
Request 2 (POST): Creates a new user.
Request 3 (PUT): Updates user data.
2. Pre-request Script in Request 1 (GET):
In the Pre-request Script of the first request (GET), set conditions to determine whether to create a new user or update existing user data:
3. Test Script in Request 2 (POST) and Request 3 (PUT):
In the Tests of the second request (POST) and third request (PUT), use postman.setNextRequest() to navigate to subsequent requests:
4. Execution:
Execute the first request (GET). Depending on the condition (whether the user exists or not), Postman will navigate to the appropriate subsequent request (either POST or PUT). After the execution of the subsequent request, the flow will continue to the first request or stop based on the conditions defined in the Test scripts.
5. Testing:
Test different scenarios by manipulating the conditions in the Pre-request Scripts and Tests. Ensure that the requests are sequenced correctly based on the conditions and the logic defined in the scripts.
How to handle Conditional Requests/Branching in Postman ?
Conditional requests or branching in Postman allows you to create logic within your requests to handle different scenarios based on conditions. This feature is useful for executing specific actions or tests depending on the response received from an API endpoint.
In this article, we’ll explore how to handle conditional requests or branching in Postman.
Table of Content
- Understanding Pre-request Scripts and Tests
- Characteristics of Pre-request Scripts
- Characteristics of Tests Scripts
- Implementing Conditional Requests in Postman
- Conclusion