TheBvrtosz
TheBvrtosz

Reputation: 95

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?

Upvotes: 3

Views: 3497

Answers (1)

Frank Borzage
Frank Borzage

Reputation: 6826

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;

Upvotes: 4

Related Questions