Using HTTP Triggers
Learn how to use HTTP triggers in Azure Functions.
In a function, an HTTP trigger allows the user to execute the function logic via an HTTP request. In this lesson, we will examine how this type of trigger works. We will do so with the help of the interactive playground below:
using System; using System.IO; using System.Net; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Newtonsoft.Json; namespace AzureFunctionApp { public class Functions { private readonly ILogger<Functions> _logger; public Functions(ILogger<Functions> log) { _logger = log; } [FunctionName("Chatbot")] [OpenApiOperation(operationId: "Run", tags: new[] { "name" })] [OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description = "The name of the user")] [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "Chat reply")] public async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "introduction")] HttpRequest req) { string name = req.Query["name"]; var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; var responseMessage = string.IsNullOrEmpty(name) ? "Hello. What is your name?" : $"Hello, {name}. What can I help you with?"; return new OkObjectResult(responseMessage); } } }
Function app with a HTTP trigger and OpenAPI
HTTP trigger basics
An HTTP trigger is added to a function method via a request parameter marked by the HttpTrigger
attribute. We have an example of it in line 30 inside the Functions.cs
file. Here are some properties that we configure inside the attribute:
AuthLevel
: This represents the authentication level for the function. In the above example, we set it asAuthorizationLevel.Anonymous
, which allows the user to execute the function without specifying any secrets in the request. ...