HTTP Triggers in Azure Functions
HTTP triggers allow you to execute Azure Functions in response to HTTP requests. This is a fundamental building block for creating serverless APIs, webhooks, and microservices with Azure Functions.
What is an HTTP Trigger?
An HTTP trigger defines an Azure Function that is invoked when an HTTP request is received by a specific endpoint. The function can then process the request, perform actions, and return an HTTP response.
Creating an HTTP Trigger
You can create an HTTP trigger in several ways, including using the Azure portal, Azure CLI, Visual Studio, or VS Code. The core configuration involves defining the function's binding, which specifies how it is triggered.
Function.json Configuration (Example)
For Node.js, C#, and other languages, the trigger configuration is often defined in a function.json
file.
{
"scriptFile": "../run.js",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Explanation:
type: "httpTrigger"
: Specifies this is an HTTP trigger.name: "req"
: The name of the parameter in your function code that will receive the HTTP request object.direction: "in"
: Indicates this binding is for input.methods: ["get", "post"]
: Restricts the HTTP methods that can trigger the function.authLevel
: Controls authorization. Options includeanonymous
,function
, andadmin
.- The second binding with
type: "http"
anddirection: "out"
is for the HTTP response.
Accessing the HTTP Request
Within your function code, you can access various properties of the incoming HTTP request, such as:
- Query parameters
- Route parameters
- Request body
- Headers
- HTTP method
Example: Node.js HTTP Trigger
Here's a simple Node.js example of an HTTP trigger that echoes a name from the query string or request body.
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
? "Hello, " + name + "!"
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
context.res = {
status: 200,
body: responseMessage
};
};
Example: C# HTTP Trigger
And here's a C# equivalent:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
public static class HttpTriggerCSharp
{
[FunctionName("HttpTriggerCSharp")]
public static async Task 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 requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}!";
return new OkObjectResult(responseMessage);
}
}
HTTP Response
Your function can return an HTTP response. This typically involves setting properties on the context.res
object (in Node.js) or returning an IActionResult
(in C#).
- Status Code: Set the HTTP status code (e.g., 200 OK, 400 Bad Request, 404 Not Found).
- Headers: Add custom headers to the response.
- Body: Include the content of the response. This can be JSON, HTML, plain text, etc.
Routing
You can define custom routes for your HTTP triggers to create cleaner API endpoints. This is configured using the route
property in the function.json
or via the Route
attribute in C#.
authLevel
for your HTTP triggers, especially for production environments, to protect your functions from unauthorized access. Use anonymous
only when necessary and with extreme caution.