Paul
Paul

Reputation: 3293

HttpTrigger not working in Azure Function

I am playing around with triggers in Azure Function

I have a function that I want 2 operations on - one is invoked via a Service Bus change, so I use ServiceBusTrigger, and the other via a HttpTrigger

I have set the function to use port 8085 via the debug settings

    public static class MyFunction
    {
        [FunctionName("MyFunction")]
        public static void Run([ServiceBusTrigger("My-topic", "MySubscription", Connection = "eventbus-connection")]string mySbMsg, ILogger log)
        {
            log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");

            var payment = JsonConvert.DeserializeObject<MyObject>(mySbMsg);
        }

        public static async Task<IActionResult> TestMethodAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "api/payments/test-method")]
            HttpRequest req, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = String.Empty;
            using (StreamReader streamReader = new StreamReader(req.Body))
            {
                requestBody = await streamReader.ReadToEndAsync();
            }

            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name ??= data?.name;

            return name != null
                ? (ActionResult) new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
}

The service bus trigger works fine, however, I always get a 404 error when I try to call the Http endpoint

I have tried

http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/api/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2
http://localhost:8085/payments/test-method?name=2

None of these work

What am I doing wrong? I know that its wrong using Anonymous as the security but I want to get it working for the basic one first then I need to see how to get the function key into the request

Paul

Upvotes: 0

Views: 4220

Answers (1)

Hury Shen
Hury Shen

Reputation: 15754

The function url should be http://localhost:8085/api/MyFunction if you do not set the value of Route (and you need to add a FunctionName for the HttpTrigger operation).

In you case, since you have set the value of Route. So your function url should be: http://localhost:8085/api/api/payments/test-method (note there are two /api).

If you want the url be http://localhost:8085/api/payments/test-method, please set Route = "payments/test-method"

Upvotes: 1

Related Questions