HTTP Triggers
HTTP triggers allow your Azure Function to be invoked via an HTTP request. This is a common way to build serverless APIs, webhooks, and microservices. When an HTTP trigger is configured, Azure Functions creates a publicly accessible URL for your function.
How HTTP Triggers Work
When a request is made to the function's URL:
- The Azure Functions runtime receives the request.
- It parses the HTTP request details (method, headers, query parameters, body).
- These details are passed as input to your function code.
- Your function code executes and can perform actions based on the request.
- The function can return an HTTP response, including status codes, headers, and a response body.
Creating an HTTP Triggered Function
You can create HTTP triggered functions using various languages and development tools. Here's a conceptual example using 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.'
: '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, /* Defaults to 200 */
body: responseMessage
};
};
Request and Response Objects
The req and res objects provide access to the incoming HTTP request and the outgoing HTTP response, respectively.
Request (req) Properties:
method: The HTTP method (GET, POST, PUT, DELETE, etc.).url: The full URL of the request.headers: An object containing request headers.query: An object containing query string parameters.params: An object containing route parameters (if defined in the route).body: The request body.originalUrl: The original URL before any routing.
Response (res) Properties:
You assign an object to context.res to define the response. Common properties include:
status: The HTTP status code (e.g., 200, 400, 404). Defaults to 200.body: The response body. Can be a string, JSON object, or stream.headers: An object containing response headers.isRaw: Set totrueif thebodyis a raw stream or buffer.
Route Configuration
HTTP triggers can be configured with specific routes to define how URLs map to functions. This allows for more complex API structures.
For example, a route like api/users/{userId} can capture the userId from the URL and make it available in req.params.
Example Route Configuration (function.json):
{
"scriptFile": "index.js",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
],
"route": "items/{itemId}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
With the above configuration, a request to /api/items/123 would have req.params.itemId set to "123".
Authentication and Authorization
Azure Functions provides several built-in authorization levels for HTTP triggers:
anonymous: No authentication required.function: Requires a function key in the request.admin: Requires an admin key in the request.
You can configure the authLevel in the function's binding configuration.
Key Concepts
- Serverless API: Build APIs without managing servers.
- Webhooks: Easily integrate with external services by receiving HTTP callbacks.
- Event-driven: Trigger function execution based on HTTP events.
- Scalability: Azure Functions automatically scales based on incoming traffic.
Next Steps
Explore different bindings to connect your functions to various Azure services and external data sources.