Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameter to azure durable function via URL Request

I have the basic durable function code:

namespace dotNetDurableFunction
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context)
        {
            var outputs = new List<string>();
            string name = context.GetInput<string>();

            // Replace "hello" with the name of your Durable Activity Function.
            outputs.Add(await context.CallActivityAsync<string>("Function1_Hello", name));

            return outputs;
        }

        [FunctionName("Function1_Hello")]
        public static string SayHello([ActivityTrigger] string name, ILogger log)
        {
            log.LogInformation($"Saying hello to {name}.");
            return $"Hello {name}!";
        }

        [FunctionName("Function1_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
            [DurableClient] IDurableOrchestrationClient starter,
            ILogger log)
        {
            //var name = await req.Content.ReadAsStringAsync();
            var name = "Bart";
            log.LogInformation(name);
            // Function input comes from the request content.
            string instanceId = await starter.StartNewAsync("Function1", name);

            log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }
}

what i would like to achieve is to call the http trigger with passing the parameter name like that: http://localhost:7071/api/Function1_HttpStart?name=Josh

and then pass the parameter from http trigger, to orchestrator and finally to the activity called by orchestrator.

Right now in this code, the output is Saying hello to . so it looks like it doesn't pass the parameter through the code, or the parameter is not being read from the request url.

Is there any way to achieve this?

like image 571
TheBvrtosz Avatar asked Apr 27 '26 00:04

TheBvrtosz


1 Answers

You can use the following code to receive name:

    var name = req.RequestUri.Query.Split("=")[1];

The following code is used to receive the request body in the post request, it cannot receive the parameters in the get request.

    var content = req.Content;
    string jsonContent = content.ReadAsStringAsync().Result;
like image 54
Frank Gong Avatar answered Apr 28 '26 15:04

Frank Gong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!