HTTP Bindings for Azure Functions
HTTP bindings enable your Azure Functions to respond to HTTP requests. They act as both a trigger and an output binding, allowing you to build web APIs and process incoming HTTP data.
Triggering an HTTP Function
The HTTP trigger allows your function to be executed in response to an HTTP request. You can configure the allowed HTTP methods (GET, POST, etc.) and specify routes.
Configuration
The HTTP trigger is typically configured in your function.json file:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
],
"route": "myapi/{paramName?}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Request Object
The incoming HTTP request is represented by a request object (named req in the example above). This object provides access to:
req.method: The HTTP method used.req.params: Route parameters.req.query: Query string parameters.req.headers: Request headers.req.get_body(): The request body.req.get_json(): The request body parsed as JSON.
Response Object
The function returns an HTTP response object (named res in the example above). You can set its properties:
res.set_status_code(status_code)res.set_headers(headers)res.set_body(body)res.set_content_type(content_type)
A common pattern is to return a JSON response:
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(
f"Hello, {name}. This HTTP triggered function executed successfully.",
status_code=200
)
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
Output Binding
The HTTP output binding is used to return an HTTP response from your function. It's often paired with the HTTP trigger.
Configuration
As seen in the trigger configuration, the http binding with direction: "out" handles the response.
Return Value
Your function should return an object that can be serialized into an HTTP response. In Python, this is typically an instance of func.HttpResponse.
Supported HTTP Methods
You can specify which HTTP methods your function should respond to in the methods array of the HTTP trigger configuration. Common methods include:
GET: Retrieve data.POST: Submit data to be processed.PUT: Update data.DELETE: Delete data.PATCH: Partially update data.
Routing
The route property in the HTTP trigger configuration allows you to define custom URL patterns for your functions. This is powerful for creating RESTful APIs.
"route": "users": Matches/api/users."route": "users/{id}": Matches/api/users/123, withidavailable inreq.params."route": "users/{id?}": Makes theidparameter optional.
Authentication and Authorization
The authLevel property in the HTTP trigger configuration controls access to your function:
anonymous: No authentication required.function: Requires a function key in the request.admin: Requires an admin key in the request.
Common Scenarios
- Building REST APIs: Use HTTP triggers and output bindings to create simple or complex web APIs.
- Webhooks: Integrate with external services by creating HTTP endpoints to receive notifications.
- Processing Form Submissions: Capture data from web forms via POST requests.
- Serving Dynamic Content: Generate HTML or JSON responses based on request parameters.