在 Azure 逻辑应用中使用 HTTP Webhook 创建和运行基于事件的自动化工作流Create and run automated event-based workflows by using HTTP webhooks in Azure Logic Apps

借助 Azure 逻辑应用和内置 HTTP Webhook 连接器,可以创建订阅服务终结点,等待特定事件以及基于这些事件运行(而不是定期检查或轮询该终结点)的自动化任务和工作流。With Azure Logic Apps and the built-in HTTP Webhook connector, you can create automated tasks and workflows that subscribe to a service endpoint, wait for specific events, and run based on those events, rather than regularly checking or polling that endpoint.

下面是一些基于 Webhook 的工作流示例:Here are some example webhook-based workflows:

  • 在触发逻辑应用运行之前,等待来自 Azure 事件中心的某个项抵达。Wait for an item to arrive from an Azure Event Hub before triggering a logic app run.
  • 在继续运行工作流之前等待批准。Wait for an approval before continuing a workflow.

本文介绍如何使用 Webhook 触发器和 Webhook 操作,以便逻辑应用可以接收和响应服务终结点上的事件。This article shows how to use the Webhook trigger and Webhook action so that your logic app can receive and respond to events at a service endpoint.

Webhook 的工作原理How do webhooks work?

Webhook 触发器基于事件,它不依赖于定期检查或轮询新项。A webhook trigger is event-based, which doesn't depend on checking or polling regularly for new items. 保存以 Webhook 触发器开头的逻辑应用,或者将逻辑应用从已禁用状态更改为已启用状态时,Webhook 触发器会通过将一个回调 URL 注册到指定服务终结点,来订阅该终结点。 When you save a logic app that starts with a webhook trigger, or when you change your logic app from disabled to enabled, the webhook trigger subscribes to the specified service endpoint by registering a callback URL with that endpoint. 然后,该触发器将等待该服务终结点调用该 URL,从而开始运行逻辑应用。The trigger then waits for that service endpoint to call the URL, which starts running the logic app. 请求触发器类似,发生指定的事件时,会立即激发逻辑应用。Similar to the Request trigger, the logic app fires immediately when the specified event happens. 如果你删除触发器并保存逻辑应用,或者将逻辑应用从已启用状态更改为已禁用状态,则 Webhook 触发器将从服务终结点取消订阅。The webhook trigger unsubscribes from the service endpoint if you remove the trigger and save your logic app, or when you change your logic app from enabled to disabled.

Webhook 操作也基于事件,它会通过将一个回调 URL 注册到指定服务终结点,来订阅该终结点。 A webhook action is also event-based and subscribes to the specified service endpoint by registering a callback URL with that endpoint. Webhook 操作将暂停逻辑应用的工作流,在逻辑应用恢复运行之前,会等待该服务终结点调用该 URL。The webhook action pauses the logic app's workflow and waits until the service endpoint calls the URL before the logic app resumes running. 在以下情况下,Webhook 操作将从服务终结点取消订阅:The webhook action unsubscribes from the service endpoint in these cases:

  • 当 Webhook 操作成功完成时When the webhook action successfully finishes
  • 如果逻辑应用运行在等待响应时已取消If the logic app run is canceled while waiting for a response
  • 在逻辑应用超时之前Before the logic app times out

例如, Office 365 Outlook 连接器的 发送审批电子邮件操作就是遵循此模式的 Webhook 操作示例。For example, the Office 365 Outlook connector's Send approval email action is an example of webhook action that follows this pattern. 可以使用 Webhook 操作将此模式扩展到任何服务中。You can extend this pattern into any service by using the webhook action.

有关详细信息,请参阅以下主题:For more information, see these topics:

有关发往逻辑应用的入站调用(例如传输层安全性(TLS,以前称为安全套接字层 (SSL))或 Azure Active Directory 开放式身份验证 (Azure AD OAuth))的加密、安全性和授权的信息,请参阅保护访问和数据 - 对基于请求的触发器的入站调用的访问For information about encryption, security, and authorization for inbound calls to your logic app, such as Transport Layer Security (TLS), previously known as Secure Sockets Layer (SSL), or Azure Active Directory Open Authentication (Azure AD OAuth), see Secure access and data - Access for inbound calls to request-based triggers.

先决条件Prerequisites

添加 HTTP Webhook 触发器Add an HTTP Webhook trigger

此内置触发器调用目标服务上的订阅终结点,并向目标服务注册回调 URL。This built-in trigger calls the subscribe endpoint on the target service and registers a callback URL with the target service. 然后,逻辑应用会等待目标服务向回调 URL 发送 HTTP POST 请求。Your logic app then waits for the target service to send an HTTP POST request to the callback URL. 发生此事件时,触发器将触发,并会将请求中的任何数据传递给工作流。When this event happens, the trigger fires and passes any data in the request along to the workflow.

  1. 登录到 Azure 门户Sign in to the Azure portal. 在逻辑应用设计器中打开空白逻辑应用。Open your blank logic app in Logic App Designer.

  2. 在设计器上的搜索框中,输入 http webhook 作为筛选器。In the designer's search box, enter http webhook as your filter. 在“触发器”列表中选择“HTTP Webhook”触发器。 From the Triggers list, select the HTTP Webhook trigger.

    选择 HTTP Webhook 触发器

    此示例将触发器重命名为 HTTP Webhook trigger,使步骤的名称更具描述性。This example renames the trigger to HTTP Webhook trigger so that the step has a more descriptive name. 此外,该示例稍后会添加 HTTP Webhook 操作,因此,两个名称必须唯一。Also, the example later adds an HTTP Webhook action, and both names must be unique.

  3. 提供要对订阅和取消订阅调用使用的 HTTP Webhook 触发器参数值。Provide the values for the HTTP Webhook trigger parameters that you want to use for the subscribe and unsubscribe calls.

    在此示例中,触发器包括执行订阅和取消订阅操作时要使用的方法、URI 和消息正文。In this example, the trigger includes the methods, URIs, and message bodies to use when performing the subscribe and unsubscribe operations.

    输入 HTTP Webhook 触发器参数

    属性Property 必选Required 说明Description
    订阅 - 方法Subscription - Method Yes 订阅目标终结点时使用的方法The method to use when subscribing to the target endpoint
    订阅 - URISubscribe - URI Yes 用于订阅目标终结点的 URLThe URL to use for subscribing to the target endpoint
    订阅 - 正文Subscribe - Body No 要包含在订阅请求中的任何消息正文。Any message body to include in the subscribe request. 此示例包括使用 @listCallbackUrl() 表达式来检索逻辑应用的回调 URL,该 URL 唯一标识订阅方(即逻辑应用)。This example includes the callback URL that uniquely identifies the subscriber, which is your logic app, by using the @listCallbackUrl() expression to retrieve your logic app's callback URL.
    取消订阅 - 方法Unsubscribe - Method No 取消订阅目标终结点时使用的方法The method to use when unsubscribing from the target endpoint
    取消订阅 - URIUnsubscribe - URI No 用于取消订阅目标终结点的 URLThe URL to use for unsubscribing from the target endpoint
    取消订阅 - 文本Unsubscribe - Body No 要包含在取消订阅请求中的可选消息正文An optional message body to include in the unsubscribe request

    注意:此属性不支持使用 listCallbackUrl() 函数。Note: This property doesn't support using the listCallbackUrl() function. 但是,触发器会自动包括并发送标头、x-ms-client-tracking-idx-ms-workflow-operation-name,目标服务可以使用这些标头来唯一标识订阅方。However, the trigger automatically includes and sends the headers, x-ms-client-tracking-id and x-ms-workflow-operation-name, which the target service can use to uniquely identify the subscriber.

  4. 若要添加其他触发器属性,请打开“添加新参数”列表。To add other trigger properties, open the Add new parameter list.

    添加更多触发器属性

    例如,如果需要使用身份验证,可以添加“订阅 - 身份验证”和“取消订阅 - 身份验证”属性 。For example, if you need to use authentication, you can add the Subscribe - Authentication and Unsubscribe - Authentication properties. 有关 HTTP Webhook 可用的身份验证类型的详细信息,请参阅向出站调用添加身份验证For more information about authentication types available for HTTP Webhook, see Add authentication to outbound calls.

  5. 继续使用触发器激发时运行的操作生成逻辑应用的工作流。Continue building your logic app's workflow with actions that run when the trigger fires.

  6. 完成后,请记得保存逻辑应用。When you're finished, done, remember to save your logic app. 在设计器工具栏上选择“保存”。On the designer toolbar, select Save.

    保存逻辑应用将在目标服务上调用订阅终结点并注册回调 URL。Saving your logic app calls the subscribe endpoint on the target service and registers the callback URL. 然后,逻辑应用会等待目标服务向回调 URL 发送 HTTP POST 请求。Your logic app then waits for the target service to send an HTTP POST request to the callback URL. 发生此事件时,触发器将触发,并会将请求中的任何数据传递给工作流。When this event happens, the trigger fires and passes any data in the request along to the workflow. 如果此操作成功完成,触发器将取消订阅终结点,并且逻辑应用将继续运行剩余的工作流。If this operation completes successfully, the trigger unsubscribes from the endpoint, and your logic app continues the remaining workflow.

添加 HTTP Webhook 操作Add an HTTP Webhook action

此内置操作调用目标服务上的订阅终结点,并向目标服务注册回调 URL。This built-in action calls the subscribe endpoint on the target service and registers a callback URL with the target service. 然后,逻辑应用会暂停并等待目标服务向回调 URL 发送 HTTP POST 请求。Your logic app then pauses and waits for target service to send an HTTP POST request to the callback URL. 发生此事件时,操作会将请求中的任何数据传递给工作流。When this event happens, the action passes any data in the request along to the workflow. 如果此操作成功完成,操作将取消订阅终结点,并且逻辑应用将继续运行剩余的工作流。If the operation completes successfully, the action unsubscribes from the endpoint, and your logic app continues running the remaining workflow.

  1. 登录到 Azure 门户Sign in to the Azure portal. 在逻辑应用设计器中打开逻辑应用。Open your logic app in Logic App Designer.

    此示例的第一步是使用 HTTP Webhook 触发器。This example uses the HTTP Webhook trigger as the first step.

  2. 在要添加 HTTP Webhook 操作的步骤下,选择“新建步骤”。Under the step where you want to add the HTTP Webhook action, select New step.

    若要在步骤之间添加操作,请将鼠标指针移到步骤之间的箭头上。To add an action between steps, move your pointer over the arrow between steps. 选择出现的加号 ( + ),然后选择“添加操作”。Select the plus sign (+) that appears, and then select Add an action.

  3. 在设计器上的搜索框中,输入 http webhook 作为筛选器。In the designer's search box, enter http webhook as your filter. 在“操作”列表中选择“HTTP Webhook”操作。 From the Actions list, select the HTTP Webhook action.

    选择 HTTP Webhook 操作

    此示例将操作重命名为“HTTP Webhook action”,使步骤的名称更具描述性。This example renames the action to "HTTP Webhook action" so that the step has a more descriptive name.

  4. 提供要对订阅和取消订阅调用使用的 HTTP Webhook 操作参数(类似于 HTTP Webhook 触发器参数)值。Provide the values for the HTTP Webhook action parameters, which are similar to the HTTP Webhook trigger parameters, that you want to use for the subscribe and unsubscribe calls.

    在此示例中,操作包括执行订阅和取消订阅操作时要使用的方法、URI 和消息正文。In this example, the action includes the methods, URIs, and message bodies to use when performing the subscribe and unsubscribe operations.

    输入 HTTP Webhook 操作参数

    属性Property 必选Required 说明Description
    订阅 - 方法Subscription - Method Yes 订阅目标终结点时使用的方法The method to use when subscribing to the target endpoint
    订阅 - URISubscribe - URI Yes 用于订阅目标终结点的 URLThe URL to use for subscribing to the target endpoint
    订阅 - 正文Subscribe - Body No 要包含在订阅请求中的任何消息正文。Any message body to include in the subscribe request. 此示例包括使用 @listCallbackUrl() 表达式来检索逻辑应用的回调 URL,该 URL 唯一标识订阅方(即逻辑应用)。This example includes the callback URL that uniquely identifies the subscriber, which is your logic app, by using the @listCallbackUrl() expression to retrieve your logic app's callback URL.
    取消订阅 - 方法Unsubscribe - Method No 取消订阅目标终结点时使用的方法The method to use when unsubscribing from the target endpoint
    取消订阅 - URIUnsubscribe - URI No 用于取消订阅目标终结点的 URLThe URL to use for unsubscribing from the target endpoint
    取消订阅 - 文本Unsubscribe - Body No 要包含在取消订阅请求中的可选消息正文An optional message body to include in the unsubscribe request

    注意:此属性不支持使用 listCallbackUrl() 函数。Note: This property doesn't support using the listCallbackUrl() function. 但是,操作会自动包括并发送标头、x-ms-client-tracking-idx-ms-workflow-operation-name,目标服务可以使用这些标头来唯一标识订阅方。However, the action automatically includes and sends the headers, x-ms-client-tracking-id and x-ms-workflow-operation-name, which the target service can use to uniquely identify the subscriber.

  5. 若要添加其他操作属性,请打开“添加新参数”列表。To add other action properties, open the Add new parameter list.

    添加更多操作属性

    例如,如果需要使用身份验证,可以添加“订阅 - 身份验证”和“取消订阅 - 身份验证”属性 。For example, if you need to use authentication, you can add the Subscribe - Authentication and Unsubscribe - Authentication properties. 有关 HTTP Webhook 可用的身份验证类型的详细信息,请参阅向出站调用添加身份验证For more information about authentication types available for HTTP Webhook, see Add authentication to outbound calls.

  6. 完成后,请记得保存逻辑应用。When you're finished, remember to save your logic app. 在设计器工具栏上选择“保存”。On the designer toolbar, select Save.

    现在,此操作运行时,逻辑应用调用目标服务上的订阅终结点并注册回调 URL。Now, when this action runs, your logic app calls the subscribe endpoint on the target service and registers the callback URL. 然后,逻辑应用会暂停工作流,并等待目标服务向回调 URL 发送 HTTP POST 请求。The logic app then pauses the workflow and waits for the target service to send an HTTP POST request to the callback URL. 发生此事件时,操作会将请求中的任何数据传递给工作流。When this event happens, the action passes any data in the request along to the workflow. 如果此操作成功完成,操作将取消订阅终结点,并且逻辑应用将继续运行剩余的工作流。If the operation completes successfully, the action unsubscribes from the endpoint, and your logic app continues running the remaining workflow.

触发器和操作输出Trigger and action outputs

下面是有关 HTTP Webhook 触发器或操作的输出的详细信息,输出中将返回以下信息:Here is more information about the outputs from an HTTP Webhook trigger or action, which returns this information:

属性名称Property name 类型Type 说明Description
headersheaders 对象object 请求中的标头The headers from the request
bodybody 对象object JSON 对象JSON object 包含请求中正文内容的对象The object with the body content from the request
状态代码status code intint 请求中的状态代码The status code from the request
状态代码Status code 说明Description
200200 确定OK
202202 已接受Accepted
400400 错误的请求Bad request
401401 未授权Unauthorized
403403 禁止Forbidden
404404 未找到Not Found
500500 内部服务器错误。Internal server error. 发生未知错误。Unknown error occurred.

连接器参考Connector reference

有关触发器和操作参数的详细信息,请参阅 HTTP Webhook 参数For more information about trigger and action parameters, which are similar to each other, see HTTP Webhook parameters.

后续步骤Next steps