【问题标题】:Unable to manual trigger my Azure Timer Trigger using httpclient post request无法使用 httpclient 发布请求手动触发我的 Azure 计时器触发器
【发布时间】:2019-08-10 15:39:01
【问题描述】:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-manually-run-non-http

我正在尝试手动触发我在 2.0 中创建并在 .net core 2.0 中开发的 Azure 计时器功能应用程序。

当我尝试点击 url 时出现 403 错误。

我通过的 apikey 来自:

【问题讨论】:

  • 在屏幕上你有我的钥匙和在同一行注释的钥匙。您要发送哪个密钥?

标签: azure azure-functions azure-functions-core-tools


【解决方案1】:

作为您提供的文章,您需要在ManageHost key下使用_master key

【讨论】:

  • 我这次尝试了_master key。但是现在我收到 400 bad request error。@JoeyCai
  • @knowdotnet,因为请求内容是json格式。因此,您需要将string content = string.Empty; 更改为string content = "{}";,它会正常工作:)
  • _master 和 "{}" 作为内容有效。将您的回复标记为答案。谢谢!
【解决方案2】:

我在针对服务总线触发的 Azure Functions 的集成测试中使用以下类。

 class AzureFunctionCaller
{
    private readonly HttpClient _httpClient;
    private readonly string _functionUri;

    public AzureFunctionCaller(string functionName)
    {
        _httpClient = new HttpClient();

        _httpClient.DefaultRequestHeaders.Add("x-functions-key","<Key>");
        _functionUri = $"<FUNCTION_ENDPOINT>/admin/functions/{functionName}";
    }

    public async Task CallViaAdminEndpoint(string content)
    {
        var httpContent = new StringContent(content, Encoding.UTF8, "application/json");

        var response = await _httpClient.PostAsync(_functionUri, httpContent);

        var responseContent = await response.Content.ReadAsStringAsync();
        Console.WriteLine($"Response content: {responseContent}");
    }
}

然后您必须以将内容放入“输入”对象的格式发送数据。

 var azureFunctionCaller = new AzureFunctionCaller("<FunctionName>");
 var obj = new
        {
            ... // properties you want to send
        };
        var jsonContent = JsonConvert.SerializeObject(new
        {
            input = JsonConvert.SerializeObject(obj)
        });
 await azureFunctionCaller.CallViaAdminEndpoint(jsonContent);`

为了解释输入属性,下面是 postman 中相同调用的样子:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 2021-01-15
    • 1970-01-01
    • 2021-01-17
    • 2022-11-27
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    相关资源
    最近更新 更多