Azure Function HTTP trigger

AussieBoy

Well-known member
Joined
Sep 7, 2020
Messages
78
Programming Experience
Beginner
Hi, I'm not sure if this is in the right place. How do I add a body to the HTTP trigger function.
Thanks,
 
I'm not really sure what you're asking. Are you saying that you already have a function and you want to know how to send data to it? If so then it would be just like any other HTTP post. That's not necessarily a .NET issue - the post could come from anywhere - but if you want to write C# code to do it then you would probably use an HttpClient, so I suggest that you look into that.
 
C#:
    public static class Function2
    {
        [FunctionName("Function2")]
        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 firstName = req.Query["firstName"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            //dynamic data = JsonConvert.DeserializeObject(requestBody);
            TestDto data = JsonConvert.DeserializeObject<TestDto>(requestBody);
            //firstName = firstName ?? data?.firstName;


            //string jsonTest = JsonConvert.SerializeObject(TestDto);



            string responseMessage = string.IsNullOrEmpty(firstName)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {firstName}";

            return new OkObjectResult(responseMessage);

        }
    }

Hi, I may have my wires crossed here. I thought the values of the json would get written to the TestDto elements, where provided.
I would like to prove this by displaying an element of the TestDto that has been passed firstName.
Thanks,
 
Back
Top Bottom