• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Get in Touch
  • About Me

How to trigger a Power Automate Flow with a HTTP Request

March 12, 2020 by Paulie Leave a Comment

Being able to trigger a flow in Power Automate with a simple HTTP request opens the door to so many possibilities. I love it! With some imagination you can integrate anything with Power Automate.

If you want an in-depth explanation of how to call Flow via HTTP take a look at this blog post on the Power Automate blog.

This post provide examples of some of the different ways that the trigger “When a HTTP request is received” can be executed:

  • PowerShell
  • curl on Windows
  • curl on Linux or Unix
  • vbscript
  • vba
  • jquery

The same flow will be executed with different tools or languages and each of them will submit this JSON:

{
	"MessageSubject": "Testing HTTP",
	"MessageBody": "Flow execution has been triggered"
}

The flow only has two steps, it receives the JSON payload and then sends me a message on Teams from the Flow bot:

Image of Flow in Microsoft Power Automate which will be triggered by the event "When a HTTP request is received"
Image of Flow Bot sending a Teams message using a flow Microsoft Power Automate

Invoke a Flow with PowerShell

It’s super easy to invoke a flow with PowerShell. Example:

$flowURI = "https://prod-118.westeurope.logic.azure.com:443/workflows/..."
$messageSubject = "Testing HTTP"
$messageBody = "Execution test from Powershell"
$params = @{"messageSubject"="$messageSubject";"messageBody"="$messageBody"}
Invoke-WebRequest -Uri $flowURI -Method POST -ContentType "application/json" -Body ($params|ConvertTo-Json)

Invoke a HTTP Flow Trigger with curl on Windows Command Line

Because of the way Windows command line interprets double quotes, they need to be escaped. So if you want to include the JSON data on the command line it gets ugly:

curl -H "Content-Type: application/json" -d "{\"messageSubject\": \"Test\",\"messageBody\": \"Executing Flow from curl in Windows Command Line\"}" "https://prod-118.westeurope.logic.azure.com:443/workflows/..."

It’s much easier to work with if you put the contents of the JSON in a file and use the following syntax:

curl -H "Content-Type: application/json" -d @c:\temp\data.json "https://prod-118.westeurope.logic.azure.com:443/workflows/..."

Invoke a HTTP Flow Trigger with curl on Linux or Unix

Things are a bit easier in Linux because there is no need to escape double quotes. So you can simply execute a flow like this:

curl -H "Content-Type: application/json" -d '{ "MessageSubject": "Testing HTTP", "MessageBody": "Flow execution has been triggered from Linux" }' 'https://prod-118.westeurope.logic.azure.com:443/workflows/...'

Invoke a HTTP Flow using VBScript

You can use this code to invoke a HTTP flow in a VBScript:

Option Explicit

Dim FlowURI, JSON, objHTTP, httpCode
FlowURI = "https://prod-118.westeurope.logic.azure.com:443/workflows/..."
JSON = Quote("{ `MessageSubject`: `Testing HTTP`, `MessageBody`: `Flow executed from VBScript` }")

set objHttp = wscript.Createobject("Msxml2.ServerXMLHTTP")
objHTTP.Open "POST",FlowURI,false
objHTTP.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
objHTTP.setRequestHeader "CharSet", "charset=UTF-8"
objHTTP.setRequestHeader "Accept", "application/json"
objHTTP.setRequestHeader "Content-Length", Len(JSON)
objHTTP.send JSON
httpCode = objHTTP.Status
Set objHTTP = nothing

Function Quote(stringToQuote)
	'Small Function to replace backticks with Double Quotes
	Quote=Replace(stringToQuote, "`", chr(34))
End Function

Invoke a HTTP flow using VBA

Almost the same as the VBScript above, you can easily execute a flow using VBA from any of the Microsoft Office Suite:

Option Explicit
Sub ExecuteFlow()

    Dim FlowURI As String, JSON As String, objHTTP As Object, httpCode As String
    FlowURI = "https://prod-118.westeurope.logic.azure.com:443/workflows/..."
    JSON = Quote("{ `MessageSubject`: `Testing HTTP`, `MessageBody`: `Flow executed from VBA` }")
    
    Set objHTTP = CreateObject("Msxml2.ServerXMLHTTP")
    objHTTP.Open "POST", FlowURI, False
    objHTTP.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
    objHTTP.setRequestHeader "CharSet", "charset=UTF-8"
    objHTTP.setRequestHeader "Accept", "application/json"
    objHTTP.setRequestHeader "Content-Length", Len(JSON)
    objHTTP.send JSON
    httpCode = objHTTP.Status
    Set objHTTP = Nothing

End Sub

Function Quote(stringToQuote)
    'Small Function to replace backticks with Double Quotes
    Quote = Replace(stringToQuote, "`", Chr(34))
End Function

Invoke a HTTP flow with jQuery

This is an example of how to execute the same flow with jQuery. I’ve also included the required HTML. Full source code on this page, send me a message!

<!DOCTYPE html>
<head>
<title>Execute Flow</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
flowURI = "https://prod-118.westeurope.logic.azure.com:443/workflows/..."
$(document).ready(function(){
    $("button").click(function(){
        flowData = {
           MessageSubject: "Testing HTTP",
           MessageBody: $("#teamsMessage").val()
        }
        $.ajax(
            {
                url: flowURI,
                data: JSON.stringify(flowData),
                processData: false,
                contentType: "application/json",
                dataType: "json",
                type: 'POST',
                complete: function(xhr, textStatus) 
                {
                    if (xhr.status == '202')
                    {
                        $("#flowStatus").append("Flow Executed");  
                        console.log(xhr.status);                                                  
                    } else 
                    {
                        $("#flowStatus").append("Flow Execution Failed");                                                                                
                        console.log(xhr.status);
                    }  
                }
            });
    });
});
</script>
</head>
<body>
    <p>
        <label>Message</label>
        <input type = "text" id ="teamsMessage" value="Send a message on teams to Paulie" />
    </p>
    <p>
        <button>Invoke Flow</button>
    </p>
<p id="flowStatus"></p>
</body>
</html>

I will keep adding more examples as I create them, but if you have one, feel free to share and I will add it to the post.

Now secure your HTTP Request Trigger with the instructions here.

Filed Under: Power Platform Tagged With: Power Automate, Powershell, VBA, VBScript

Reader Interactions

Leave a Reply Cancel reply

Primary Sidebar

Link to my LinkedIn Profile
Buy me a coffee
Image link to all Power Automate content

Excellent Power Automate Blogs

  • Damien Bird
  • Dennis (Expiscornovus)
  • Tom Riha

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 253 other subscribers.

Go to mobile version