Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

pass parameters from logic app email trigger to azure function http trigger issue

I have a azure logic app that triggers when an email shows up, then I have that linked to a http triggered azure function. I am trying to read a few parameters in the function, passed from the logic app. can someone tell me what I am doing wrong.

screenshot of logic setup

code in azure function

        [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 gets populated correctly but attachmentName is ignored. I think it has something to do with the last line of code. data.name. I don't understand what that line is doing.

question from:https://stackoverflow.com/questions/65833688/pass-parameters-from-logic-app-email-trigger-to-azure-function-http-trigger-issu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the test, req.Query obtains parameters in the form of <your-url>? parameter1 = value1&parameter2 = value2. In other words, req.Query can only get the parameters in the Get request.

You are sending a post request, so using req.Query cannot get the parameters you want.

I think it has something to do with the last line of code. data.name. I don't understand what that line is doing.

This code is to judge whether the name is empty, if it is empty, then name=data?name.

req.Query did not get the value of name, so this code gets the value of name from data?.name, so this is the reason why name has a value but attachmentName has no value in your result .

The correct code should look like this:

    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);
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...