使用 Azure 逻辑应用调用 REST 终结点Call REST endpoints by using Azure Logic Apps

借助 Azure 逻辑应用和内置的 HTTP + Swagger 连接器,可以通过生成逻辑应用,使用一个 Swagger 文件自动执行定期调用任何 REST 终结点的工作流。With Azure Logic Apps and the built-in HTTP + Swagger connector, you can automate workflows that regularly call any REST endpoint through a Swagger file by building logic apps. HTTP + Swagger 触发器和操作的工作方式与 HTTP 触发器和操作相同,但通过公开 Swagger 文件描述的 API 结构和输出,在逻辑应用设计器中提供更好的体验。The HTTP + Swagger trigger and action work the same as the HTTP trigger and action but provide a better experience in the Logic App Designer by exposing the API structure and outputs described by the Swagger file. 如果要实现轮询触发器,请遵循创建用于调用逻辑应用的其他 API、服务和系统的自定义 API 中所述的轮询模式。To implement a polling trigger, follow the polling pattern that's described in Create custom APIs to call other APIs, services, and systems from logic apps.

先决条件Prerequisites

添加 HTTP + Swagger 触发器Add an HTTP + Swagger trigger

此内置触发器将一个 HTTP 请求发送到用于描述 REST API 的 Swagger 文件的 URL,并返回包含该文件的内容的响应。This built-in trigger sends an HTTP request to a URL for a Swagger file that describes a REST API and returns a response that contains that file's content.

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

  2. 在设计器的搜索框中,输入“swagger”作为筛选器。On the designer, in the search box, enter "swagger" as your filter. 在“触发器”列表中选择“HTTP + Swagger”触发器。 From the Triggers list, select the HTTP + Swagger trigger.

    选择 HTTP + Swagger 触发器

  3. 在“SWAGGER 终结点 URL”框中,输入 Swagger 文件的 URL,然后选择“下一步”。 In the SWAGGER ENDPOINT URL box, enter the URL for the Swagger file, and select Next.

    对于认知服务人脸 API,此示例使用位于“中国东部 2”区域的 Swagger URL:This example uses the Swagger URL that is located in the China East 2 region for the Cognitive Services Face API:

    https://chinaeast2.dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0

    此屏幕截图显示了带有“HTTP + Swagger”触发器的逻辑应用设计器,其中“Swagger 终结点 URL”属性设置为一个 URL 值。

  4. 当设计器显示了 Swagger 文件描述的操作时,请选择要使用的操作。When the designer shows the operations described by the Swagger file, select the operation that you want to use.

    此屏幕截图显示了逻辑应用设计器,其中包含一个“HTTP + Swagger”触发器和一个显示 Swagger 操作的列表。

  5. 提供要包含在终结点调用中的触发器参数的值(根据所选的操作而异)。Provide the values for the trigger parameters, which vary based on the selected operation, that you want to include in the endpoint call. 设置重复周期,以确定触发器调用终结点的频率。Set up the recurrence for how often you want the trigger to call the endpoint.

    此示例将触发器重命名为“HTTP + Swagger trigger:Face - Detect”,使步骤名称更具描述性。This example renames the trigger to "HTTP + Swagger trigger: Face - Detect" so that the step has a more descriptive name.

    此屏幕截图显示了带有“HTTP + Swagger”触发器(显示“人脸 - 检测”操作)的逻辑应用设计器。

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

    有关 HTTP + Swagger 可用的身份验证类型的详细信息,请参阅向出站调用添加身份验证For more information about authentication types available for HTTP + Swagger, see Add authentication to outbound calls.

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

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

添加 HTTP + Swagger 操作Add an HTTP + Swagger action

此内置操作对用于描述 REST API 的 Swagger 文件的 URL 发出一个 HTTP 请求,并返回包含该文件的内容的响应。This built-in action makes an HTTP request to the URL for the Swagger file that describes a REST API and returns a response that contains that file's content.

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

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

    选择 HTTP + Swagger 操作

  4. 在“SWAGGER 终结点 URL”框中,输入 Swagger 文件的 URL,然后选择“下一步”。 In the SWAGGER ENDPOINT URL box, enter the URL for the Swagger file, and select Next.

    此示例将位于 China East 2 区域的 Swagger URL 用于认知服务人脸 APIThis example uses the Swagger URL that is located in the China East 2 region for the Cognitive Services Face API:

    https://chinaeast2.dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0

    输入 Swagger 终结点的 URL

  5. 当设计器显示了 Swagger 文件描述的操作时,请选择要使用的操作。When the designer shows the operations described by the Swagger file, select the operation that you want to use.

    Swagger 文件中的操作

  6. 提供要包含在终结点调用中的操作参数的值(根据所选的操作而异)。Provide the values for the action parameters, which vary based on the selected operation, that you want to include in the endpoint call.

    此示例未使用参数,而是将操作重命名为“HTTP + Swagger action:Face - Identify”,使步骤名称更具描述性。This example has no parameters, but renames the action to "HTTP + Swagger action: Face - Identify" so that the step has a more descriptive name.

    操作详细信息

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

    有关 HTTP + Swagger 可用的身份验证类型的详细信息,请参阅向出站调用添加身份验证For more information about authentication types available for HTTP + Swagger, see Add authentication to outbound calls.

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

在 Azure 存储中托管 SwaggerHost Swagger in Azure Storage

可以引用非托管的或者不符合安全要求和跨域要求的 Swagger 文件,方法是将该文件上传到 Azure 存储帐户中的 Blob 容器,并在该存储帐户中启用 CORS。You can reference a Swagger file that's not hosted or that doesn't meet the security and cross-origin requirements by uploading that file to blob container in an Azure storage account and enabling CORS on that storage account. 若要在 Azure 存储中创建、设置和存储 Swagger 文件,请执行以下步骤:To create, set up, and store Swagger files in Azure Storage, follow these steps:

  1. 创建 Azure 存储帐户Create an Azure storage account.

  2. 现在,为 Blob 启用 CORS。Now enable CORS for the blob. 在存储帐户的菜单中选择“CORS”。On your storage account's menu, select CORS. 在“Blob 服务”选项卡上指定值,然后选择“保存”。 On the Blob service tab, specify these values, and then select Save.

    属性Property Value
    允许的源Allowed origins *
    允许的方法Allowed methods GET, HEAD, PUTGET, HEAD, PUT
    允许的标头Allowed headers *
    公开的标头Exposed headers *
    最大期限(以秒为单位)Max age (in seconds) 200

    此示例使用 Azure 门户,不过,你也可以使用 Azure 存储资源管理器之类的工具,或使用此示例 PowerShell 脚本自动配置此设置。Although this example uses the Azure portal, you can use a tool such as Azure Storage Explorer, or automatically configure this setting by using this sample PowerShell script.

  3. 创建 Blob 容器Create a blob container. 在容器的“概述”窗格中,选择“更改访问级别”。 On the container's Overview pane, select Change access level. 在“公共访问级别”列表中,选择“Blob (仅限对 Blob 进行匿名读取访问)”,然后选择“确定”。 From the Public access level list, select Blob (anonymous read access for blobs only), and select OK.

  4. 通过 Azure 门户Azure 存储资源管理器将 Swagger 文件上传到 Blob 容器Upload the Swagger file to the blob container, either through the Azure portal or Azure Storage Explorer.

  5. 若要在 Blob 容器中引用该文件,请从 Azure 存储资源管理器获取采用以下格式(区分大小写)的 HTTPS URL:To reference the file in the blob container, get the HTTPS URL that follows this format, which is case-sensitive, from Azure Storage Explorer:

    https://<storage-account-name>.blob.core.chinacloudapi.cn/<blob-container-name>/<complete-swagger-file-name>?<query-parameters>

连接器参考Connector reference

下面是有关 HTTP + Swagger 触发器或操作的输出的详细信息。Here is more information about the outputs from an HTTP + Swagger trigger or action. HTTP + Swagger 调用返回以下信息:The HTTP + Swagger call returns this information:

属性名称Property name 类型Type 说明Description
headersheaders objectobject 请求中的标头The headers from the request
bodybody objectobject 包含请求中正文内容的对象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.

后续步骤Next steps