Power Automate
HTTP triggers in Power Automate explained

HTTP triggers in Power Automate explained

Power Automate flows can be triggered with HTTP triggers and it will be useful in many scenarios. There are HTTP actions available inside a flow that can be used to get or post data to an external system, In the Azure Form recognizer example, we have used HTTP get request. Similarly, we have used HTTP triggers in the IQBot example.

HTTP trigger flows can be used whenever a certain trigger point is not available from the existing connectors like a button click on a custom page or Suppose if we need to pass data to SharePoint from any external application we can use this method, of course, SharePoint has Rest API methods but we need to configure the security headers to access SharePoint data.

We will see the Post and Get methods works, Before proceeding get to know about HTTP methods available and how it works in general.

POST Method

Normally Post method is used to create/update data in the server, In this example, we will create a CSV file in the flow passed over the HTTP request.

Add the “When an HTTP request is received” trigger, create a JSON structure in which the data will be passed, and click done automatically it will create the Request Body JSON Schema and select the method as Post.

Here we created two properties “fileName” is to pass the name of the file and “csv” is to pass the base64 format of the file.

//Sample JSON Payload
{
    "fileName":"",
    "csv":""
}

//Constructed JSON Schema
{
    "type": "object",
    "properties": {
        "fileName": {
            "type": "string"
        },
        "csv": {
            "type": "string"
        }
    }
}

Add a new action and save the workflow, a URL will be generated like below which is the endpoint to trigger the flow.

//Generated URL
https://xxx.logic.azure.com:443/workflows/axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=Kt4LjEVVKZvEjK3Z975lf2FxWXToxcoxxRm7XQQNGAk


//URL Explanation
https://xxx.logic.azure.com:443/workflows/{logicapp-resource-ID}/triggers/manual/paths/invoke?&api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig={shared-access-signature}

Once the flow is saved it will create an app and register in the Azure and it is mapped in the logicapp-resource-ID.

Construct the “csv” text back to the CSV file in the compose action then pass it to the “Create file” action.

Create CSV

To test this flow, convert the CSV file to base64 format, base64.guru is a good site to convert the files for testing purposes.

To test the HTTP request we will use the req.bin site, It does not require any installation.

Enter the URL and select the method as Post, Here we don’t have any authorization and in the content pass the JSON in the required format like below.

The status is returned as 202 which means the HTTP request is accepted since there is no response message to indicate the outcome of the request.

The workflow is triggered and the file is created in the onedrive.

Let’s add a response action and pass the return values in the JSON format.

Response Action

The result will look like below

GET Method

Generally, Get method is used to request data from a specified datasource, Both Get and Post can be used to pass the data but the difference is in the Get method the values are passed over the URL and in the Post method values are passed in the JSON format.

Create a flow with an HTTP trigger and set the method as GET and in the relative path add URL variables. The generated URL will come with the relativePath.

Get Request
//Get Request
https://prod-142.westus.logic.azure.com/workflows/2ad88275db06410380968d24c2e13369/triggers/manual/paths/invoke/ID/{ID}?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=B9Wd5FnaMXDy64IBaTn9OvNqC6LjLZBh4xnYWcfoOaM

We will get a record from the SharePoint list based on the ID passed and add the output in the response action.

Since it is a get request the ID can be passed in the URL and the output can be seen in the browser itself.

Seems very simple right, In Power Automate each action in the backend will be converted as an API request, If you see the Peek code option the JSON will be pretty clear. Below is the peek code JSON of the response action.

{
    "kind": "Http",
    "inputs": {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json"
        },
        "body": {
            "ID": "@outputs('Get_item')?['body/ID']",
            "Title": "@outputs('Get_item')?['body/Title']"
        }
    },
    "metadata": {
        "operationMetadataId": "6556865d-3761-4018-b60a-550b49727071"
    }
}

One thing we missed here is the security, Anyone with this URL can trigger the flow which is not a good way to implement, We will see how to secure an HTTP request in a separate post.

There are scenarios where we need to pass data from an external system, like when anything is created or updated in that system. In those cases, we have to implement webhooks in that system and that should be connected with HTTP triggers.

Please post your queries in the comment section. Happy Building šŸ™‚

0

Leave a Reply

%d bloggers like this: