Loops for pagination
You can use Postman Flows to build a “while do” loop that’s well-suited to pagination use cases.
The loops covered in the previous topics about loops and loops with external data all run through an entire list or for a specified number of iterations. Other than waiting for them to complete, you can’t short circuit or break out of those loops.
This topic’s pagination use case requires a different design pattern: one that enables you break out of a loop:
-
Update a value within the loop, then use an HTTP Request block to send the value to and retrieve it from the Postman Echo API. This enables your flow to manipulate state across multiple iterations of the loop.
-
Within the loop, use an Evaluate block to increment or otherwise changes the value and a Condition block to decide whether the loop should iterate or terminate based on the new value.
Build a loop for pagination
Here’s how the finished loop will look:
Before building the flow, create an HTTP GET request in a collection of your choice. Configure the request with the following URL:
Rename the request “Current page number”.
Create a new flow.
Construct the loop
In this section you’ll add blocks to build a loop, including a Condition block to determine whether to continue iterating or stop.
-
Create an HTTP Request block, choose the “Current page number” HTTP request you created above. This renames the block as Current page number.
Make sure the Start block isn’t connected to this or any other block.
-
Connect a Number block to the value input of the Current page number block. Leave its value at
0. -
Connect an Evaluate block to the Current page number block’s Success output and rename it “Turn the page”. Change
value1to “pageBeforeTurning”. In the inline Select block, enter the pathbody.args.pageNumber. -
Paste the following expression into the TypeScript window:
-
Connect a Delay block to the
Resultoutput of the Turn the page block and set its value to 1500 milliseconds. -
Connect a Condition block to the Delay block and rename it “Stop after page 5”. Rename
value1asonPageand paste the following expression into the Condition 1 window:This expression returns true when the page number exceeds 5. When that happens, the loop terminates. As long as the page number is less than or equal to 5, the loop keeps iterating.
-
Run the flow so that output values appear for each block. In the output of the Stop after page 5 block, under default, click and drag
onPageto create a Select block that outputs the page number value. -
Connect the output of the new Select block to the
valueinput of the Current page number block. Note that this connection loops back to the beginning, making the flow a true loop in form, not only in function.
Add blocks to display status
The blocks you add in this section will enable you to follow the loop’s action throughout its lifecycle.
-
Connect a Template block to the
Successoutput of the Current page number block and rename it Message when iterating. RenamekeyasonPage. In the inline Select block, enter the pathbody.args.pageNumber. Paste the following expression into the text window: -
Connect a Display block to the Message when iterating block. From
Additional Settings, select Text size > Large.
-
Connect a Template block to the Condition 1 output of the Stop after page 5 block and rename it Message when terminating. Rename
keyaslastPage. In the inline Select block, clickEnter path...and chooseonPage. -
Paste the following expression into the text window:
-
Connect the Display block you created earlier to the Message when terminating block.
Run the flow. You’ll see each block activate in sequence until the loop goes beyond page 5 and terminates.