Processing HTTP Request Response | Do Until Loop | Power Automate


Introduction: 

Power Automate, formerly known as Microsoft Flow, is a powerful automation platform that allows users to create workflows to automate tasks across various applications and services. In this blog, we will explore how to send an HTTP request using Power Automate and then apply a Do Until loop on the JSON response to efficiently handle data processing and task automation. This combination of HTTP requests and loops can be particularly useful when working with APIs or web services that return JSON data.

Step 1: Sign in to Power Automate

  1. Open your web browser and navigate to the Power Automate website (https://flow.microsoft.com/).
  2. Sign in using your Microsoft account or work/school account.


Step 2: Create a New Flow

  1. Click on the "Create" button in the top navigation bar.
  2. Select "Instant cloud flow" to create a new flow from scratch.


Step 3: Trigger the Flow

  1. Choose a suitable trigger for your flow. For this example, we will use the "Manually Trigger a Flow" trigger to run the flow at a set interval.
  2. Configure the name as per your requirements.

Click "Create"

Step 4: Add the HTTP Request Action

  1. Click on the "+ New step" button.
  2. Search for "HTTP" in the action search bar and select "HTTP" from the list of actions.
  3. In the HTTP action, specify the method (GET, POST, etc.), URI, and any required headers or parameters for your specific API or web service.

Step 5: Parse the JSON Response

  1. After the HTTP action, add a new action by clicking "+ New step."

Configure the HTTP Request: You can use the following for demo:

URIhttps://dummyjson.com/products

Method: Get


If we run this request we get the following response:





  1. Add a new step called compose

    2. Click on add dynamic content and go to expression

Write the following expression:

length(body('HTTP')?['products'])

Click Update




If we save and run the flow, we get the count of record in our response:




Step 6: Apply the Do Until Loop

  1. Add a new action after the "compose" action.
  2. Search for "Do Until" in the action search bar and select "Control - Do Until."


  1. In the "Condition" field, specify the condition for the loop to continue. For example, you can use a variable and length of response.


  1. Inside the "Do Until" loop, include the actions that you want to execute repeatedly until the condition is met.

Step 7: Handle Data and Loop Exit

  1. Add a new step called "increment a variable" and increment at the end of all the actions inside the loop.

To get a single record from the response inside the loop you can add a new compose action before the increment variable step:

Write the following expression to get the record:
body('HTTP')?['products']?[variables('loopCount')]



If we save and test the flow, you can see we now have a single record inside the compose 2 step.


In order to get the title of the current record we can add a new compose 

Write the following expression as input:
outputs('Compose_2')?['title']

Click on OK



Step 8: Save and Test the Flow

  1. Click the "Save" button in the top-right corner to save your flow.
  2. Test the flow to ensure it runs without any errors. You can use sample data or real data from your API to validate the behavior of the flow.
We are now able to get title of every record from our http request response

Conclusion: 

In this blog, we've explored how to send an HTTP request in Power Automate and apply a Do Until loop on the JSON response. By combining these powerful features, you can automate complex tasks that involve working with APIs or web services efficiently. Power Automate's user-friendly interface and extensive integration capabilities make it an ideal choice for automating repetitive tasks and improving overall productivity. So go ahead and explore the possibilities with Power Automate to streamline your workflows and save time on repetitive manual processes. Happy automating!

 


Comments