您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

手动运行非 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 to a way to send a request to Azure to run the function. 用于发出该请求的 URL 采用特定的形式。The URL used to make this request takes a specific form.

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

  • 主机名: 函数应用的公共位置,由函数应用的名称和 azurewebsites.net 或自定义域组成。Host name: The function app's public location that is made up from the function app's name plus azurewebsites.net 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

在 Azure 门户中导航到函数,单击“管理” ,找到“主机密钥”部分。Navigate to your function in the Azure portal and click on Manage and find the Host Keys section. 单击 _master 行中的“复制”按钮,将主密钥复制到剪贴板。 Click on the Copy button in the _master row to copy the master key to your clipboard.

从“函数管理”屏幕复制主密钥

复制主密钥以后,单击函数名称,返回到代码文件窗口。After copying the master key, click on the function name to return to the code file window. 接下来,单击“日志”选项卡。 手动从 Postman 运行已记录函数时,会在此处看到该函数中的消息。Next, click on the Logs tab. 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.

调用该函数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. 单击“标头”选项卡。 Click on the Headers tab.

  4. 输入 x-functions-key 作为第一个密钥,并将主密钥(从剪贴板)粘贴到“值”框中。 Enter x-functions-key as the first key and paste the master key (from the clipboard) into the value box.

  5. 输入 Content-Type 作为第二个密钥,然后输入 application/json 作为Enter Content-Type as the second key and enter application/json as the value.

    Postman 标头设置

  6. 单击“正文”选项卡。 Click on the Body tab.

  7. 输入 { "input": "test" } 作为请求的正文。Enter { "input": "test" } as the body for the request.

    Postman 正文设置

  8. 单击“Send” 。Click Send.

    通过 Postman 发送请求

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

接下来,返回到 Azure 门户中的函数。Next, return to your function in the Azure portal. 找到“日志”窗口,此时会看到手动调用函数后出来的消息。 Locate the Logs window and you'll see messages coming from the manual call to the function.

函数日志源于手动调用

后续步骤Next steps