HTTP Trigger Bindings for Azure Functions
This document provides detailed information about HTTP trigger and output bindings for Azure Functions.
HTTP triggers are a fundamental part of serverless applications, enabling event-driven architectures.
Overview
The HTTP trigger allows your Azure Function to be invoked via an HTTP request. It supports various HTTP methods (GET, POST, PUT, DELETE, etc.) and allows you to easily process incoming requests and send back HTTP responses. This binding makes Azure Functions ideal for building web APIs, microservices, and webhook integrations.
Request and Response Objects
When an HTTP trigger is invoked, the function receives an `HttpRequest` object and can return an `HttpResponse` object. These objects provide access to request details like headers, query parameters, body, and allow you to construct a custom response.
HttpRequest Properties
Method: The HTTP method of the request (e.g., "GET", "POST").Url: The URL of the request.Headers: A dictionary of request headers.Query: A dictionary of query parameters.Body: The request body as a stream or parsed object.Path: The path component of the URL.Route: The matched route template, if any.
HttpResponse Properties
StatusCode: The HTTP status code for the response (e.g., 200, 404).Body: The response body. Can be a string, JSON, or other serializable types.Headers: A dictionary of response headers.IsRaw: A boolean indicating whether the body should be treated as raw content.
Configuration
The HTTP trigger is configured in your `function.json` file. You can specify the route, allowed methods, and other parameters.
Example `function.json`
{
"scriptFile": "run.csx",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
],
"route": "api/items/{id?}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Key Configuration Properties
authLevel: Determines the authorization level required to invoke the function. Options includeanonymous,function,admin, andsystem.methods: An array of allowed HTTP methods for this trigger. If omitted, all methods are allowed.route: A custom route template. If not specified, the route defaults to the function's folder name. Parameters can be defined using curly braces (e.g.,{id}).
Programming Examples
Here are examples demonstrating how to use the HTTP trigger in different languages.
C# Example
using System.Net.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.HttpFeatures;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
public static class HttpExample
{
[FunctionName("HttpExample")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "hello/{name}")] HttpRequestMessage req,
string name,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string queryName = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
string nameToDisplay = name ?? queryName ?? "world";
string responseMessage = $"Hello, {nameToDisplay}!";
return new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent(responseMessage, System.Text.Encoding.UTF8, "text/plain")
};
}
}
JavaScript Example (Node.js)
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
};
};
Output Bindings
The HTTP output binding is used to return an HTTP response from your function. It's configured with direction: "out" and type: "http".
Common Scenarios
- Returning JSON data for APIs.
- Sending plain text responses.
- Returning different status codes based on processing results (e.g.,
201 Created,400 Bad Request,404 Not Found). - Setting custom response headers.
Advanced Topics
function.json to manage routes and authorization levels.