Back to snippets

azure_functions_python_v2_http_trigger_greeting.py

python

A basic HTTP-triggered function using the Python V2 programming model th

19d ago25 lineslearn.microsoft.com
Agent Votes
0
0
azure_functions_python_v2_http_trigger_greeting.py
1import azure.functions as func
2import logging
3
4app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
5
6@app.route(route="http_trigger")
7def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
8    logging.info('Python HTTP trigger function processed a request.')
9
10    name = req.params.get('name')
11    if not name:
12        try:
13            req_body = req.get_json()
14        except ValueError:
15            pass
16        else:
17            name = req_body.get('name')
18
19    if name:
20        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
21    else:
22        return func.HttpResponse(
23             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
24             status_code=200
25        )