HTTP Triggers and Bindings
HTTP triggers and bindings are fundamental to building web APIs and webhook integrations with Azure Functions. They allow your functions to be invoked via HTTP requests and to respond with HTTP responses.
HTTP Trigger
An HTTP trigger allows your function to be executed in response to an HTTP request. You can configure the allowed HTTP methods (GET, POST, PUT, DELETE, etc.) and routes.
Configuration
In your function.json file, the HTTP trigger is defined as follows:
{
"scriptFile": "run.csx",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
],
"route": "items/{id}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
type: Must behttpTrigger.direction: Must beinfor a trigger.name: The name of the parameter in your function code that will receive the request object.methods: An array of allowed HTTP methods.route: An optional route template. This allows you to define custom URLs for your function, including parameters like{id}.authLevel: Specifies how the HTTP endpoint is secured. Common values includeanonymous,function, andadmin.
HTTP Output Binding
The HTTP output binding is used to return an HTTP response from your function. It's often paired with an HTTP trigger.
Configuration
The corresponding output binding in function.json:
{
"type": "http",
"direction": "out",
"name": "res"
}
type: Must behttp.direction: Must beoutfor an output binding.name: The name of the parameter in your function code that will be used to set the HTTP response.
Accessing Request Data
When using an HTTP trigger, the req parameter (or whatever name you've specified) will be an object representing the incoming HTTP request. You can access various parts of the request:
req.query: Query parameters.req.body: Request body (parsed as JSON if the content type is appropriate).req.headers: Request headers.req.params: Route parameters (if defined in the route).req.method: The HTTP method of the request.
Returning an HTTP Response
You can return an HTTP response by setting the value of the output binding parameter (res in our example). The response object typically includes:
status: The HTTP status code (e.g., 200, 400, 404).body: The content of the response body.headers: Response headers.
Example (JavaScript)
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: { message: responseMessage },
headers: {
'Content-Type': 'application/json'
}
};
};
Example (C#)
using System.IO;
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 HttpExample
{
[FunctionName("HttpExample")]
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(new { message = responseMessage });
}
}
Common HTTP Scenarios
- REST APIs: Building endpoints for CRUD operations.
- Webhooks: Receiving notifications from external services.
- Proxies: Aggregating multiple functions or external services behind a single HTTP endpoint.