【问题标题】:pass parameters from logic app email trigger to azure function http trigger issue将参数从逻辑应用电子邮件触发器传递到 azure 函数 http 触发器问题
【发布时间】:2021-04-26 05:50:17
【问题描述】:

我有一个 azure 逻辑应用程序,当电子邮件出现时触发,然后我将它链接到 http 触发的 azure 函数。我正在尝试读取从逻辑应用程序传递的函数中的一些参数。谁能告诉我我做错了什么。

天蓝色函数中的代码

        [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];
        string attachmentName = req.Query["attachmentName"];
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);


        name = name ?? data?.name;

name 被正确填充,但 attachmentName 被忽略。我认为这与最后一行代码有关。数据名称。我不明白那条线在做什么。

【问题讨论】:

    标签: azure-functions azure-logic-apps


    【解决方案1】:

    根据测试,req.Query&lt;your-url&gt;? parameter1 = value1&amp;parameter2 = value2的形式获取参数。也就是说req.Query只能获取Get请求中的参数。

    你正在发送一个post请求,所以使用req.Query无法得到你想要的参数。

    我认为这与最后一行代码有关。数据名称。我不明白那条线在做什么。

    这段代码是判断name是否为空,如果为空则name=data?name。

    req.Query 没有得到name 的值,所以这段代码从data?.name 得到name 的值,所以这就是为什么name 有值但attachmentName 在你的结果中没有值的原因。

    正确的代码应该是这样的:

        public static class Function1
        {
            [FunctionName("Function1")]
            public static async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
                ILogger log)
            {
                log.LogInformation("C# HTTP trigger function processed a request.");
    
                string name = req.Query["name"];
                string attachmentName = req.Query["attachmentName"];
                
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic data = JsonConvert.DeserializeObject(requestBody);
                
                name = name ?? data?.name;
                attachmentName = attachmentName ?? data?.attachmentName;
    
                log.LogInformation(name);
                log.LogInformation(attachmentName);
    
                return new OkObjectResult(name);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 2022-10-07
      • 2020-10-22
      • 1970-01-01
      • 2019-02-15
      相关资源
      最近更新 更多