从 Azure 逻辑应用通过 HTTP 或 HTTPS 调用服务终结点Call service endpoints over HTTP or HTTPS from Azure Logic Apps

使用 Azure 逻辑应用和内置 HTTP 触发器或操作,可以创建自动化任务和工作流,可通过 HTTP 或 HTTPS 向其他服务和系统上的终结点发送出站请求。With Azure Logic Apps and the built-in HTTP trigger or action, you can create automated tasks and workflows that can send outbound requests to endpoints on other services and systems over HTTP or HTTPS. 若要改为接收和响应入站 HTTPS 调用,请使用内置请求触发器和响应操作To receive and respond to inbound HTTPS calls instead, use the built-in Request trigger and Response action.

例如,可按特定的计划检查网站的服务终结点,从而对该终结点进行监视。For example, you can monitor a service endpoint for your website by checking that endpoint on a specific schedule. 当该终结点上发生特定的事件(例如网站关闭)时,该事件会触发逻辑应用的工作流并运行该工作流中的操作。When the specified event happens at that endpoint, such as your website going down, the event triggers your logic app's workflow and runs the actions in that workflow.

  • 若要按定期计划检查或轮询某个终结点,可添加 HTTP 触发器作为工作流中的第一个步骤。To check or poll an endpoint on a recurring schedule, add the HTTP trigger as the first step in your workflow. 每次触发器检查终结点时,触发器都会调用该终结点或向该终结点发送请求。Each time that the trigger checks the endpoint, the trigger calls or sends a request to the endpoint. 该终结点的响应确定了逻辑应用的工作流是否运行。The endpoint's response determines whether your logic app's workflow runs. 触发器将终结点响应中的任何内容传递到逻辑应用中的操作。The trigger passes any content from the endpoint's response to the actions in your logic app.

  • 若要从工作流中的任何其他位置调用终结点,请添加 HTTP 操作To call an endpoint from anywhere else in your workflow, add the HTTP action. 该终结点的响应确定了工作流剩余操作的运行方式。The endpoint's response determines how your workflow's remaining actions run.

本文介绍如何使用 HTTP 触发器和 HTTP 操作,以便逻辑应用可以将出站调用发送到其他服务和系统。This article shows how to use the HTTP trigger and HTTP action so that your logic app can send outbound calls to other services and systems.

有关来自逻辑应用的出站调用(例如传输层安全性(TLS,以前称为安全套接字层 (SSL))、自签名证书或 Azure Active Directory 开放式身份验证 (Azure AD OAuth))的加密、安全性和授权的信息,请参阅保护访问和数据 - 对其他服务和系统的出站调用的访问For information about encryption, security, and authorization for outbound calls from your logic app, such as Transport Layer Security (TLS), previously known as Secure Sockets Layer (SSL), self-signed certificates, or Azure Active Directory Open Authentication (Azure AD OAuth), see Secure access and data - Access for outbound calls to other services and systems.

先决条件Prerequisites

添加 HTTP 触发器Add an HTTP trigger

此内置触发器对终结点的指定 URL 发出 HTTP 调用,并返回响应。This built-in trigger makes an HTTP call to the specified URL for an endpoint and returns a response.

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

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

    选择 HTTP 触发器

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

  3. 提供要包含在目标终结点调用中的 HTTP 触发器参数的值。Provide the values for the HTTP trigger parameters that you want to include in the call to the target endpoint. 设置重复周期,以确定触发器检查目标终结点的频率。Set up the recurrence for how often you want the trigger to check the target endpoint.

    选择 HTTP 触发器

    如果选择的身份验证类型不是“None”,则身份验证设置将根据你的选择而有所不同。If you select an authentication type other than None, the authentication settings differ based on your selection. 有关 HTTP 可用的身份验证类型的详细信息,请参阅以下主题:For more information about authentication types available for HTTP, see these topics:

  4. 若要添加其他可用参数,请打开“添加新参数”列表,并选择所需的参数。To add other available parameters, open the Add new parameter list, and select the parameters that you want.

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

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

添加 HTTP 操作Add an HTTP action

此内置操作对终结点的指定 URL 发出 HTTP 调用,并返回响应。This built-in action makes an HTTP call to the specified URL for an endpoint and returns a response.

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

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

  2. 在要添加 HTTP 操作的步骤下,选择“新建步骤”。Under the step where you want to add the HTTP 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. 在“选择操作”下,选择“内置”。 Under Choose an action, select Built-in. 在搜索框中,输入 http 作为筛选器。In the search box, enter http as your filter. 在“操作”列表中,选择“HTTP”操作。 From the Actions list, select the HTTP action.

    选择 HTTP 触发器

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

  4. 提供要包含在目标终结点调用中的 HTTP 操作参数的值。Provide the values for the HTTP action parameters that you want to include in the call to the target endpoint.

    选择 HTTP 触发器

    如果选择的身份验证类型不是“None”,则身份验证设置将根据你的选择而有所不同。If you select an authentication type other than None, the authentication settings differ based on your selection. 有关 HTTP 可用的身份验证类型的详细信息,请参阅以下主题:For more information about authentication types available for HTTP, see these topics:

  5. 若要添加其他可用参数,请打开“添加新参数”列表,并选择所需的参数。To add other available parameters, open the Add new parameter list, and select the parameters that you want.

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

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

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

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

具有多部分/表单数据类型的内容Content with multipart/form-data type

若要处理 HTTP 请求中具有 multipart/form-data 类型的内容,可以使用此格式向 HTTP 请求的正文添加包含 $content-type$multipart 属性的 JSON 对象。To handle content that has multipart/form-data type in HTTP requests, you can add a JSON object that includes the $content-type and $multipart attributes to the HTTP request's body by using this format.

"body": {
   "$content-type": "multipart/form-data",
   "$multipart": [
      {
         "body": "<output-from-trigger-or-previous-action>",
         "headers": {
            "Content-Disposition": "form-data; name=file; filename=<file-name>"
         }
      }
   ]
}

例如,假设你有一个逻辑应用,它使用该站点的 API(支持 multipart/form-data 类型)向网站发送对 Excel 文件的 HTTP POST 请求。For example, suppose you have a logic app that sends an HTTP POST request for an Excel file to a website by using that site's API, which supports the multipart/form-data type. 下面是此操作的可能外观:Here's how this action might look:

选择 HTTP 触发器

以下是在基础工作流定义中显示 HTTP 操作的 JSON 定义的同一示例:Here is the same example that shows the HTTP action's JSON definition in the underlying workflow definition:

"HTTP_action": {
   "inputs": {
      "body": {
         "$content-type": "multipart/form-data",
         "$multipart": [
            {
               "body": "@trigger()",
               "headers": {
                  "Content-Disposition": "form-data; name=file; filename=myExcelFile.xlsx"
               }
            }
         ]
      },
      "method": "POST",
      "uri": "https://finance.contoso.com"
   },
   "runAfter": {},
   "type": "Http"
}

具有 application/x-www-form-urlencoded 类型的内容Content with application/x-www-form-urlencoded type

若要在 HTTP 请求的正文中提供 form-urlencoded 数据,必须指定数据具有 application/x-www-form-urlencoded 内容类型。To provide form-urlencoded data in the body for an HTTP request, you have to specify that the data has the application/x-www-form-urlencoded content type. 在 HTTP 触发器或操作中,添加 content-type 标头。In the HTTP trigger or action, add the content-type header. 将标头值设置为 application/x-www-form-urlencodedSet the header value to application/x-www-form-urlencoded.

例如,假设你有一个逻辑应用,它向支持 application/x-www-form-urlencoded 类型的网站发送 HTTP POST 请求。For example, suppose you have a logic app that sends an HTTP POST request to a website, which supports the application/x-www-form-urlencoded type. 下面是此操作的可能外观:Here's how this action might look:

选择 HTTP 触发器

异步请求-响应行为Asynchronous request-response behavior

默认情况下,Azure 逻辑应用中所有基于 HTTP 的操作都遵循标准异步操作模式By default, all HTTP-based actions in Azure Logic Apps follow the standard asynchronous operation pattern. 该模式指定在 HTTP 操作调用某个终结点、服务、系统或 API 或向其发送请求后,接收方立即返回“202 已接受”响应。This pattern specifies that after an HTTP action calls or sends a request to an endpoint, service, system, or API, the receiver immediately returns a "202 ACCEPTED" response. 此代码确认接收方已接受请求,但尚未完成处理。This code confirms that the receiver accepted the request but hasn't finished processing. 响应可以包括一个指定了 URL 和刷新 ID 的 location 标头,调用方可以使用该标头来轮询或检查异步请求的状态,直到接收方停止处理并返回“200 正常”成功响应或其他非 202 响应。The response can include a location header that specifies the URL and a refresh ID that the caller can use to poll or check the status for the asynchronous request until the receiver stops processing and returns a "200 OK" success response or other non-202 response. 但是,调用方不必等待请求完成处理即可继续运行下一操作。However, the caller doesn't have to wait for the request to finish processing and can continue to run the next action. 有关详细信息,请参阅异步微服务集成强制实施微服务自治For more information, see Asynchronous microservice integration enforces microservice autonomy.

  • 在逻辑应用设计器中,HTTP 操作(而不是触发器)有一个默认启用的异步模式设置。In the Logic App Designer, the HTTP action, but not trigger, has an Asynchronous Pattern setting, which is enabled by default. 此设置指定调用方不等待处理完成即可继续执行下一操作,但需继续检查状态直到处理停止。This setting specifies that the caller doesn't wait for processing to finish and can move on to the next action but continues checking the status until processing stops. 如果禁用,则此设置指定调用方需等待处理完成才能继续执行下一操作。If disabled, this setting specifies that the caller waits for processing to finish before moving on to the next action.

    若要查找此设置,请执行以下步骤:To find this setting, follow these steps:

    1. 在 HTTP 操作的标题栏上,选择省略号 ( ... ) 按钮,这将打开操作的设置。On the HTTP action's title bar, select the ellipses (...) button, which opens the action's settings.

    2. 找到“异步模式”设置。Find the Asynchronous Pattern setting.

      “异步模式”设置

  • HTTP 操作的基础 JavaScript 对象表示法 (JSON) 定义隐式遵循异步操作模式。The HTTP action's underlying JavaScript Object Notation (JSON) definition implicitly follows the asynchronous operation pattern.

禁用异步操作Disable asynchronous operations

有时,你在特定场景下可能需要 HTTP 操作的异步行为,例如,当你希望实现以下目的时:Sometimes, you might want to the HTTP action's asynchronous behavior in specific scenarios, for example, when you want to:

关闭“异步模式”设置Turn off Asynchronous Pattern setting

  1. 在逻辑应用设计器的 HTTP 操作的标题栏上,选择省略号 ( ... ) 按钮,这将打开操作的设置。In the Logic App Designer, on the HTTP action's title bar, select the ellipses (...) button, which opens the action's settings.

  2. 找到“异步模式”设置,关闭此设置(如果已启用),然后选择“完成”。Find the Asynchronous Pattern setting, turn the setting to Off if enabled, and select Done.

    禁用“异步模式”设置

在操作的 JSON 定义中禁用异步模式Disable asynchronous pattern in action's JSON definition

在 HTTP 操作的基础 JSON 定义中,向操作的定义添加 "DisableAsyncPattern" 操作选项,使操作改为遵循同步操作模式。In the HTTP action's underlying JSON definition, add the "DisableAsyncPattern" operation option to the action's definition so that the action follows the synchronous operation pattern instead. 有关详细信息,另请参阅在同步操作模式下运行操作For more information, see also Run actions in a synchronous operation pattern.

避免长时间运行的任务发生 HTTP 超时Avoid HTTP timeouts for long-running tasks

HTTP 请求有一个超时限制HTTP requests have a timeout limit. 如果有长时间运行的 HTTP 操作由于此限制而超时,则可使用以下选项:If you have a long-running HTTP action that times out due to this limit, you have these options:

  • 禁用 HTTP 操作的异步操作模式,使该操作不会持续轮询或检查请求的状态,Disable the HTTP action's asynchronous operation pattern so that the action doesn't continually poll or check the request's status. 而是等待接收方在请求完成处理后以状态和结果做出响应。Instead, the action waits for the receiver to respond with the status and results after the request finishes processing.

  • 将 HTTP 操作替换为 HTTP Webhook 操作,后者会等待接收方在请求完成处理后以状态和结果做出响应。Replace the HTTP action with the HTTP Webhook action, which waits for the receiver to respond with the status and results after the request finishes processing.

禁止检查位置标头Disable checking location headers

某些终结点、服务、系统或 API 会返回没有 location 标头的“202 已接受”响应。Some endpoints, services, systems, or APIs return a "202 ACCEPTED" response that don't have a location header. 若要避免 HTTP 操作在 location 标头不存在时不断检查请求状态,可以使用以下选项:To avoid having an HTTP action continually check the request status when the location header doesn't exist, you can have these options:

  • 禁用 HTTP 操作的异步操作模式,使该操作不会持续轮询或检查请求的状态,Disable the HTTP action's asynchronous operation pattern so that the action doesn't continually poll or check the request's status. 而是等待接收方在请求完成处理后以状态和结果做出响应。Instead, the action waits for the receiver to respond with the status and results after the request finishes processing.

  • 将 HTTP 操作替换为 HTTP Webhook 操作,后者会等待接收方在请求完成处理后以状态和结果做出响应。Replace the HTTP action with the HTTP Webhook action, which waits for the receiver to respond with the status and results after the request finishes processing.

已知问题Known issues

忽略了 HTTP 标头Omitted HTTP headers

如果 HTTP 触发器或操作包含这些标头,则逻辑应用会从生成的请求消息中删除这些标头,且不显示任何警告或错误:If an HTTP trigger or action includes these headers, Logic Apps removes these headers from the generated request message without showing any warning or error:

  • Accept-* 标头(Accept-version 除外)Accept-* headers except for Accept-version
  • Allow
  • Content-* 中含以下例外:Content-DispositionContent-EncodingContent-TypeContent-* with these exceptions: Content-Disposition, Content-Encoding, and Content-Type
  • Cookie
  • Expires
  • Host
  • Last-Modified
  • Origin
  • Set-Cookie
  • Transfer-Encoding

尽管逻辑应用不会阻止保存此类逻辑应用(使用具有这些标头的 HTTP 触发器或操作的逻辑应用),但逻辑应用会忽略这些标头。Although Logic Apps won't stop you from saving logic apps that use an HTTP trigger or action with these headers, Logic Apps ignores these headers.

连接器参考Connector reference

有关触发器和操作参数的详细信息,请参阅以下部分:For more information about trigger and action parameters, see these sections:

后续步骤Next steps