Mastering HTTP Triggers in Power Automate

Fernanda Ek - May 17 '23 - - Dev Community

HTTP triggers in Power Automate provide a valuable functionality for initiating flows in various scenarios such as retrieving or submitting data to an external system. HTTP trigger flows become particularly useful when an existing connector lacks a specific trigger point. Additionally, these HTTP triggers are handy when data needs to be passed from an external application/third-party applications and other services like SharePoint.

In other words, if you want to trigger your flow from a connector that's not available in Power Automate, this technique is the way to go.


Scenario

I created an SPFx extension that I named 'Suggestion Box', and I want to be able to trigger a Power Automate flow from this SPFx application.

Image description


Instant flow - Trigger

Select "When an HTTP request is received" trigger

Image description

Click on 'Use sample payload to generate schema' to create a JSON structure for passing the data. Once done, it will automatically generate the Request Body JSON Schema, and you can select the 'Post' method.

Image description

Click "Done" and the JSON schema will be generated.

Image description

When this endpoint is triggered, it requires three properties: siteUrl, pageTitle, and suggestionBody

Action

Specifically for my application, I want the flow to send me the suggestionBody via email.
And now, if we look at the Dynamic content, we can see that we have the siteUrl, pageTitle, and suggestionBody variables.

Image description

Click "Save" and the "HTTP POST URL" will be created

Image description
Add a new step "Response"-ation select cthe "Body" property from the dynamic content

Image description

Test the HTTP request with Postman

  • Select POST as method

  • Copy the HTTP POST URL you got when you saved the flow and specify the endpoint

  • Create a JSON sample in the body

  • Add header: Content-type=application/json
    Send the request and check your email

Image description

Image description

Invoke the Power Automate flow from SPFx solution

Import the following code snippet in your tsx file:

import { HttpClient, HttpClientResponse, IHttpClientOptions } from '@microsoft/sp-http';
It will import the necessary modules (HttpClient, HttpClientResponse and IHttpClientOptions) from the @microsoft/sp-http package.

Add the following function to trigger the Power Automate flow

Note: Replace the "postURL" with the actual HTTP POST URL generated in your trigger.



async function sendSuggestion(
siteUrl: string,
pageTitle: string,
suggestionBody: string
): Promise<HttpClientResponse> {

    const postURL = &lt;PASTE HERE HTTP POST URL&gt;;

    const body: string = JSON.stringify({
        siteUrl: siteUrl,
        pageTitle: pageTitle,
        suggestionBody: suggestionBody,
      });

      const requestHeaders: Headers = new Headers();

      requestHeaders.append('Content-type', 'application/json');

      const httpClientOptions: IHttpClientOptions = {
        body: body,
        headers: requestHeaders,
      };

      console.log("Sending Suggestion");

      return props.context.httpClient.post(
        postURL,
        HttpClient.configurations.v1,
        httpClientOptions
      ).then((response: HttpClientResponse): Promise&lt;HttpClientResponse&gt; =&gt; {

        console.log("Suggestion sent.");

        return response.json();

      });
    }
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode




Voilà!

Image description
On click of "Save Suggestion"-button it triggers the Power Automate flow and the below mail is received.

Image description
Now you can use Power Automate to create custom HTTP triggers that can be invoked by your application whenever needed.

Let me know in the comments below about your experiences integrating it into your workflows

. . . . . . . . . . . . . . .
Terabox Video Player