手动运行非 HTTP 触发的函数Manually run a non HTTP-triggered function

本文演示如何通过特殊格式的 HTTP 请求手动运行非 HTTP 触发的函数。This article demonstrates how to manually run a non HTTP-triggered function via specially formatted HTTP request.

在某些上下文中,可能需要“按需”运行间接触发的 Azure Functions。In some contexts, you may need to run "on-demand" an Azure Function that is indirectly triggered. 例如,按计划函数或者根据其他资源的操作来运行的函数属于间接触发的函数。Examples of indirect triggers include functions on a schedule or functions that run as the result of another resource's action.

以下示例中使用了 Postman,但也可以使用 cURLFiddler 或任何其他类似的工具来发送 HTTP 请求。Postman is used in the following example, but you may use cURL, Fiddler or any other like tool to send HTTP requests.

定义请求位置Define the request location

若要运行非 HTTP 触发的函数,需要通过某种方式向 Azure 发送运行函数的请求。To run a non HTTP-triggered function, you need a way to send a request to Azure to run the function. 用于发出该请求的 URL 采用特定的形式。The URL used to make this request takes a specific form.

定义请求位置:主机名 + 文件夹路径 + 函数名

  • 主机名: 函数应用的公共位置,由函数应用的名称和“chinacloudsites.cn”或自定义域组成。Host name: The function app's public location that is made up from the function app's name plus chinacloudsites.cn or your custom domain.
  • 文件夹路径: 若要通过 HTTP 请求访问非 HTTP 触发的函数,需通过 admin/functions 文件夹发送请求。Folder path: To access non HTTP-triggered functions via an HTTP request, you have to send the request through the folders admin/functions.
  • 函数名: 要运行的函数的名称。Function name: The name of the function you want to run.

请将 Postman 中的此请求位置与函数发送给 Azure 的请求中的主密钥配合使用,以便运行函数。You use this request location in Postman along with the function's master key in the request to Azure to run the function.

备注

在本地运行时,不需要函数的主密钥。When running locally, the function's master key is not required. 可以直接调用函数,省略 x-functions-key 标头。You can directly call the function omitting the x-functions-key header.

获取函数的主密钥Get the function's master key

  1. Azure 门户中导航到函数应用,选择“应用密钥”,然后选择 _master 密钥。Navigate to your function app in the Azure portal, select App Keys, and then the _master key.

    找到要复制的主密钥。

  2. 在“编辑密钥”部分中,将密钥值复制到剪贴板,然后选择“确定”。In the Edit key section, copy the key value to your clipboard, and then select OK.

    将主密钥复制到剪贴板。

  3. 复制主密钥之后,选择“代码 + 测试”,然后选择“日志”。After copying the _master key, select Code + Test, and then select Logs. 手动从 Postman 运行已记录函数时,会在此处看到该函数中的消息。You'll see messages from the function logged here when you manually run the function from Postman.

    查看日志以了解主密钥测试结果。

注意

由于函数应用中提升的权限由主密钥授予,因此不应与第三方共享此密钥或在应用程序中分发此密钥。Due to the elevated permissions in your function app granted by the master key, you should not share this key with third parties or distribute it in an application. 此密钥应仅发送到 HTTPS 终结点。The key should only be sent to an HTTPS endpoint.

调用该函数Call the function

打开 Postman 并执行以下步骤:Open Postman and follow these steps:

  1. 在 URL 文本框中输入请求位置Enter the request location in the URL text box.

  2. 确保 HTTP 方法设置为 POSTEnsure the HTTP method is set to POST.

  3. 选择“标头”选项卡。Select the Headers tab.

  4. 键入“x-functions-key”作为第一个密钥,并粘贴主密钥(在剪贴板中)作为值。Type x-functions-key as the first key and paste the master key (from the clipboard) as the value.

  5. 键入“Content-Type”作为第二个密钥,然后键入“application/json”作为值。Type Content-Type as the second key and type application/json as the value.

    Postman 标头设置。

  6. 选择“正文”选项卡。Select the Body tab.

  7. 键入“{ "input": "test" }”作为请求的正文。Type { "input": "test" } as the body for the request.

    Postman 正文设置。

  8. 选择“发送”。Select Send.

    通过 Postman 发送请求。

    Postman 然后会报告状态“202 已接受”。Postman then reports a status of 202 Accepted.

  9. 接下来,返回到 Azure 门户中的函数。Next, return to your function in the Azure portal. 查看日志,你会看到手动调用函数后显示的消息。Review the logs and you'll see messages coming from the manual call to the function.

    查看日志以了解主密钥测试结果。

后续步骤Next steps