在 Azure 逻辑应用中接收和响应入站 HTTPS 请求Receive and respond to inbound HTTPS requests in Azure Logic Apps

借助 Azure 逻辑应用和内置的请求触发器和响应操作,可以创建可通过 HTTPS 接收入站请求的自动化任务和工作流。With Azure Logic Apps and the built-in Request trigger and Response action, you can create automated tasks and workflows that can receive inbound requests over HTTPS. 若要改为发送出站请求,请使用内置 HTTP 触发器或 HTTP 操作To send outbound requests instead, use the built-in HTTP trigger or HTTP action.

例如,使用逻辑应用,你可以完成以下操作:For example, you can have your logic app:

  • 接收并响应对本地数据库中数据的 HTTPS 请求。Receive and respond to an HTTPS request for data in an on-premises database.

  • 发生外部 Webhook 事件时触发工作流。Trigger a workflow when an external webhook event happens.

  • 接收来自其他逻辑应用的 HTTPS 调用并对其作出响应。Receive and respond to an HTTPS call from another logic app.

本文介绍如何使用请求触发器和响应操作,以便逻辑应用可以接收和响应入站调用。This article shows how to use the Request trigger and Response action so that your logic app can receive and respond to inbound calls.

有关发往逻辑应用的入站调用(例如传输层安全性(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

添加请求触发器Add Request trigger

此内置触发器创建可手动调用的终结点,该终结点只能处理 HTTPS 上的入站请求。This built-in trigger creates a manually callable endpoint that can handle only inbound requests over HTTPS. 当调用方将请求发送到此终结点时,请求触发器会激发并运行逻辑应用。When a caller sends a request to this endpoint, the Request trigger fires and runs the logic app. 有关如何调用此触发器的详细信息,请参阅在 Azure 逻辑应用中使用 HTTPS 终结点调用、触发或嵌套工作流For more information about how to call this trigger, see Call, trigger, or nest workflows with HTTPS endpoints in Azure Logic Apps.

逻辑应用仅在有限的时间内使入站请求保持打开状态。Your logic app keeps an inbound request open only for a limited time. 假设逻辑应用包含响应操作,如果逻辑应用在此时间之后未向调用方发回响应,则逻辑应用会将 504 GATEWAY TIMEOUT 状态返回给调用方。Assuming that your logic app includes a Response action, if your logic app doesn't send a response back to the caller after this time passes, your logic app returns a 504 GATEWAY TIMEOUT status to the caller. 如果逻辑应用不包含响应操作,If your logic app doesn't include a Response action,

则逻辑应用立即将 202 ACCEPTED 状态返回给调用方。your logic app immediately returns a 202 ACCEPTED status to the caller.

  1. 登录 Azure 门户Sign in to the Azure portal. 创建空白逻辑应用。Create a blank logic app.

  2. 逻辑应用设计器打开后,在搜索框中,输入 http request 作为筛选器。After Logic App Designer opens, in the search box, enter http request as your filter. 从触发器列表中选择“当收到 HTTP 请求时”触发器。From the triggers list, select the When an HTTP request is received trigger.

    选择请求触发器

    请求触发器显示以下属性:The Request trigger shows these properties:

    选择请求触发器

    属性名称Property name JSON 属性名称JSON property name 必须Required 说明Description
    HTTP POST URLHTTP POST URL {无}{none} Yes 保存逻辑应用后生成的终结点 URL,用于调用逻辑应用The endpoint URL that's generated after you save the logic app and is used for calling your logic app
    请求正文 JSON 架构Request Body JSON Schema schema No 描述传入请求正文中的属性和值的 JSON 架构The JSON schema that describes the properties and values in the incoming request body
  3. 在“请求正文 JSON 架构”框中,有选择性地输入一个用于描述传入请求中的正文的 JSON 架构,例如:In the Request Body JSON Schema box, optionally enter a JSON schema that describes the body in the incoming request, for example:

    选择请求触发器

    设计器将使用此架构为请求中的属性生成标记。The designer uses this schema to generate tokens for the properties in the request. 这样,逻辑应用就可以通过触发器分析、使用请求中的数据并将其传递到工作流。That way, your logic app can parse, consume, and pass along data from the request through the trigger into your workflow.

    下面是示例架构:Here is the sample schema:

    {
      "type": "object",
      "properties": {
         "account": {
            "type": "object",
            "properties": {
               "name": {
                  "type": "string"
               },
               "ID": {
                  "type": "string"
               },
               "address": {
                  "type": "object",
                  "properties": {
                     "number": {
                        "type": "string"
                     },
                     "street": {
                        "type": "string"
                     },
                     "city": {
                        "type": "string"
                     },
                     "state": {
                        "type": "string"
                     },
                     "country": {
                        "type": "string"
                     },
                     "postalCode": {
                        "type": "string"
                     }
                  }
               }
            }
         }
      }
    }
    

    输入 JSON 架构时,设计器将提醒你在请求中包含 Content-Type 标头,并将该标头值设置为 application/jsonWhen you enter a JSON schema, the designer shows a reminder to include the Content-Type header in your request and set that header value to application/json. 有关详细信息,请参阅处理内容类型For more information, see Handle content types.

    提醒包含“Content-Type”标头

    下面是此标头的 JSON 格式外观:Here's what this header looks like in JSON format:

    {
      "Content-Type": "application/json"
    }
    

    若要生成基于预期有效负载(数据)的 JSON 架构,可以使用 JSONSchema.net 之类的工具,也可以执行以下步骤:To generate a JSON schema that's based on the expected payload (data), you can use a tool such as JSONSchema.net, or you can follow these steps:

    1. 在请求触发器中,选择“使用示例有效负载生成架构”。In the Request trigger, select Use sample payload to generate schema.

      选择了“使用示例有效负载生成架构”的屏幕截图

    2. 输入示例有效负载,然后选择“完成”。Enter the sample payload, and select Done.

      选择请求触发器

      下面是示例有效负载:Here is the sample payload:

      {
       "account": {
          "name": "Contoso",
          "ID": "12345",
          "address": { 
             "number": "1234",
             "street": "Anywhere Street",
             "city": "AnyTown",
             "state": "AnyState",
             "country": "USA",
             "postalCode": "11111"
          }
       }
      }
      
  4. 若要检查入站调用是否具有与指定架构匹配的请求正文,请执行以下步骤:To check that the inbound call has a request body that matches your specified schema, follow these steps:

    1. 在请求触发器的标题栏中,选择省略号按钮 ( ... )。In the Request trigger's title bar, select the ellipses button (...).

    2. 在触发器的设置中,开启“架构验证”,然后选择“完成”。In the trigger's settings, turn on Schema Validation, and select Done.

      如果入站调用的请求正文与架构不匹配,则触发器会返回“HTTP 400 Bad Request”错误。If the inbound call's request body doesn't match your schema, the trigger returns an HTTP 400 Bad Request error.

  5. 若要添加其他属性,请打开“添加新参数”列表,并选择要添加的参数。To specify additional properties, open the Add new parameter list, and select the parameters that you want to add.

    属性名称Property name JSON 属性名称JSON property name 必须Required 说明Description
    方法Method method No 传入的请求在调用逻辑应用时必须使用的方法The method that the incoming request must use to call the logic app
    相对路径Relative path relativePath No 逻辑应用终结点 URL 可接受的参数的相对路径The relative path for the parameter that the logic app's endpoint URL can accept

    本示例添加了 Method 属性:This example adds the Method property:

    选择请求触发器

    Method 属性显示在触发器中,使你可以从列表中选择方法。The Method property appears in the trigger so that you can select a method from the list.

    选择请求触发器

  6. 现在,添加另一个操作作为工作流中的下一步骤。Now, add another action as the next step in your workflow. 在触发器下,选择“下一步骤”,以便可以找到要添加的操作。Under the trigger, select Next step so that you can find the action that you want to add.

    例如,可以通过添加响应操作来响应请求,该响应操作可用于返回自定义响应,本主题后面部分将介绍此相关内容。For example, you can respond to the request by adding a Response action, which you can use to return a customized response and is described later in this topic.

    逻辑应用仅在有限的时间内使传入请求保持打开状态。Your logic app keeps the incoming request open only for a limited time. 假设逻辑应用工作流包含响应操作,如果该逻辑应用在此时间之后没有返回响应,则逻辑应用会将 504 GATEWAY TIMEOUT 返回给调用方。Assuming that your logic app workflow includes a Response action, if the logic app doesn't return a response after this time passes, your logic app returns a 504 GATEWAY TIMEOUT to the caller. 否则,如果逻辑应用不包含响应操作,则逻辑应用会立即将 202 ACCEPTED 响应返回给调用方。Otherwise, if your logic app doesn't include a Response action, your logic app immediately returns a 202 ACCEPTED response to the caller.

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

    此步骤生成一个 URL,用于发送触发逻辑应用的请求。This step generates the URL to use for sending the request that triggers the logic app. 若要复制此 URL,请选择 URL 旁边的复制图标。To copy this URL, select the copy icon next to the URL.

    选择请求触发器

    备注

    若要在调用请求触发器时在 URI 中包含哈希符号或井号(“#”),请改用此编码版本:%25%23If you want to include the hash or pound symbol (#) in the URI when making a call to the Request trigger, use this encoded version instead: %25%23

  8. 若要触发逻辑应用,请将 HTTP POST 发送到生成的 URL。To trigger your logic app, send an HTTP POST to the generated URL.

    例如,可以使用 Postman 等工具来发送 HTTP POST。For example, you can use a tool such as Postman to send the HTTP POST. 有关触发器的基础 JSON 定义以及如何调用此触发器的详细信息,请参阅以下主题:请求触发器类型通过 Azure 逻辑应用中的 HTTP 终结点调用、触发或嵌套工作流For more information about the trigger's underlying JSON definition and how to call this trigger, see these topics, Request trigger type and Call, trigger, or nest workflows with HTTP endpoints in Azure Logic Apps.

触发器输出Trigger outputs

下面是有关“请求”触发器的输出的详细信息:Here's more information about the outputs from the Request trigger:

JSON 属性名称JSON property name 数据类型Data type 说明Description
headers ObjectObject 描述请求中的标头的 JSON 对象A JSON object that describes the headers from the request
body ObjectObject 描述请求中的正文内容的 JSON 对象A JSON object that describes the body content from the request

添加响应操作Add a Response action

使用请求触发器处理入站请求时,可以使用内置响应操作对响应进行建模并将有效负载结果发送回调用方。When you use the Request trigger to handle inbound requests, you can model the response and send the payload results back to the caller by using the built-in Response action. 只能将响应操作与请求触发器配合使用。You can use the Response action only with the Request trigger. 这种与请求触发器和响应操作的组合会创建 [请求-响应模式]( https://en.wikipedia.org (此网站在 AZURE 中国云上不可用)/wiki/Request%E2%80%93response)。This combination with the Request trigger and Response action creates the [request-response pattern](https://en.wikipedia.org (THIS WEB SITE IS NOT AVAILABLE ON AZURE CHINA CLOUD) /wiki/Request%E2%80%93response). 除了 Foreach 循环和循环内以及并行分支以外,可以在工作流中的任意位置添加响应操作。Except for inside Foreach loops and Until loops, and parallel branches, you can add the Response action anywhere in your workflow.

重要

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

  • Allow
  • Content-* 中含以下例外:Content-DispositionContent-EncodingContent-TypeContent-* with these exceptions: Content-Disposition, Content-Encoding, and Content-Type
  • Cookie
  • Expires
  • Last-Modified
  • Set-Cookie
  • Transfer-Encoding

尽管逻辑应用不会阻止保存具有这些标头的响应操作的逻辑应用,但逻辑应用会忽略这些标头。Although Logic Apps won't stop you from saving logic apps that have a Response action with these headers, Logic Apps ignores these headers.

  1. 在逻辑应用设计器中,在要添加响应操作的步骤下,选择“新步骤”。In the Logic App Designer, under the step where you want to add a Response action, select New step.

    例如,使用前面所述的“请求”触发器:For example, using the Request trigger from earlier:

    选择请求触发器

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

  2. 在“选择操作”下的搜索框中,输入 response 作为筛选器,然后选择“响应”操作。Under Choose an action, in the search box, enter response as your filter, and select the Response action.

    选择请求触发器

    为简明起见,本示例中“请求”触发器已折叠。The Request trigger is collapsed in this example for simplicity.

  3. 添加响应消息所需的任何值。Add any values that are required for the response message.

    单击某些字段中的框会打开动态内容列表。In some fields, clicking inside their boxes opens the dynamic content list. 然后,可以从工作流中的前面步骤选择表示可用输出的标记。You can then select tokens that represent available outputs from previous steps in the workflow. 在上述示例中指定的架构内的属性现在会显示在动态内容列表中。Properties from the schema specified in the earlier example now appear in the dynamic content list.

    例如,对于“标头”框,请包含 Content-Type 作为键名称,并将键值设置为 application/json,如本主题前面所述。For example, for the Headers box, include Content-Type as the key name, and set the key value to application/json as mentioned earlier in this topic. 对于“正文”框,可以从动态内容列表中选择触发器正文输出。For the Body box, you can select the trigger body output from the dynamic content list.

    选择请求触发器

    若要查看 JSON 格式的标头,请选择“切换到文本视图”。To view the headers in JSON format, select Switch to text view.

    选择请求触发器

    下面是有关可在“响应”操作中设置的属性的详细信息。Here is more information about the properties that you can set in the Response action.

    属性名称Property name JSON 属性名称JSON property name 必须Required 说明Description
    状态代码Status Code statusCode Yes 要在响应中返回的状态代码The status code to return in the response
    标头Headers headers No 一个 JSON 对象,描述要包含在响应中的一个或多个标头A JSON object that describes one or more headers to include in the response
    正文Body body No 响应正文The response body
  4. 若要添加其他属性,例如响应正文的 JSON 架构,请打开“添加新参数”列表,并选择要添加的参数。To specify additional properties, such as a JSON schema for the response body, open the Add new parameter list, and select the parameters that you want to add.

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

后续步骤Next steps