Iterate through a list using a For loop
A common Flows design pattern involves getting a response from a request that returns a list, then processing each item in the list. You can do this by constructing a loop with a For block. A For block requires an array as its input. Blocks such as List, Get Variable, Evaluate, and Select can return an array, but the response from an HTTP Request block typically doesn’t. You can use TypeScript in an Evaluate block to transform a response into an array.
Recipe
The following recipe shows how to use an Evaluate block to manipulate the response from an HTTP Request to create an array:
-
Create a flow and add an HTTP Request block.
-
In the HTTP Request block, create a GET request with the following URL:
This gets a list of five book titles, along with other response data.
-
Connect an Evaluate block to the HTTP Request block’s Success port. Rename the
value1inputitems, then click its Enter path… field and set the path tobody.works. -
Click the dropdown list at the top of the Evaluate block and select TypeScript. Then, to make an array from the selected part of the previous block’s response, enter this code in the Evaluate block:
This creates an array of the book titles.
-
Connect a For block’s List port to the Evaluate block’s Result port.
-
Connect an Evaluate block to the For block’s Item port and rename the
value1inputitem. Select TypeScript from the dropdown list at the top of the block, then enter this code:This capitalizes all the letters in the titles.
-
Connect a Collect block to the Evaluate block’s Result port. This will put the capitalized items back into a list. If you added the For block from the block menu, Postman already added a Collect block next to it, so connect that block instead of adding another one.
Try it out
To see this recipe’s completed flow, check out Cookbook: Iterate through a list using a For loop. You can also clone this flow to your workspace to experiment with it.
Related pages
This recipe shows one example of how to make a loop in Flows. For more examples, see the following: