Microsoft Docs

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

HttpResponse Properties

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

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

Advanced Topics

Note: Ensure your function's URL is correctly configured and accessible. Use the function.json to manage routes and authorization levels.

Related Topics