Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Create an Azure Function with HTTP Trigger
📖 Scenario: You are building a simple Azure Function that responds to HTTP requests. This function will greet users by name when they send a request with their name.
🎯 Goal: Build an Azure Function with an HTTP trigger that reads a name parameter from the query string and returns a greeting message.
📋 What You'll Learn
Create a function named HttpTriggerFunction with an HTTP trigger.
Configure the function to accept GET requests.
Read the name parameter from the query string.
Return a greeting message including the name parameter.
💡 Why This Matters
🌍 Real World
Azure Functions with HTTP triggers are commonly used to build serverless APIs that respond to web requests without managing servers.
💼 Career
Understanding how to create and configure HTTP-triggered Azure Functions is essential for cloud developers and DevOps engineers working with serverless architectures.
Progress0 / 4 steps
1
Create the function.json file with HTTP trigger
Create a file named function.json with an HTTP trigger binding. Set the authLevel to function and allowed methods to ["get"].
Azure
Hint
The function.json file defines the trigger and output bindings for your Azure Function.
2
Create the function code file with the main function
Create a file named __init__.py and define a function named main that accepts parameters req and context. Import func from azure.functions.
Azure
Hint
The main function is the entry point for your Azure Function.
3
Read the 'name' parameter from the HTTP request
Inside the main function, read the name parameter from the query string using req.params.get('name'). Store it in a variable called name.
Azure
Hint
Use req.params.get('name') to get the query parameter.
4
Return a greeting message in the HTTP response
Complete the main function to return an HttpResponse with the message "Hello, {name}!" if name is provided. If name is missing, return "Please pass a name on the query string." with status code 400.
Azure
Hint
Use an if statement to check if name exists and return the appropriate HttpResponse.
Practice
(1/5)
1. What does an HTTP trigger do in an Azure Function?
easy
A. It stores data for the function to use later.
B. It runs the function when it receives a web request.
C. It schedules the function to run at specific times.
D. It sends emails automatically when triggered.
Solution
Step 1: Understand the role of HTTP triggers
HTTP triggers start a function when a web request is received.
Step 2: Compare with other triggers
Other triggers like timers schedule functions, but HTTP triggers respond to web calls.
Final Answer:
It runs the function when it receives a web request. -> Option B
Quick Check:
HTTP trigger = runs on web request [OK]
Hint: HTTP trigger means function runs on web request [OK]
Common Mistakes:
Confusing HTTP trigger with timer trigger
Thinking HTTP trigger stores data
Assuming HTTP trigger sends emails
2. Which of the following is the correct way to define an HTTP trigger in an Azure Function's function.json file?
A. The output binding type should be "http" not "httpTrigger".
B. The output binding type "http" is invalid; it should be "httpResponse".
C. The output binding type "http" is invalid; it should be "httpTrigger".
D. The output binding type should be "http" with direction "in".
Solution
Step 1: Review input and output bindings
The input binding uses "httpTrigger" which is correct for HTTP triggers.
Step 2: Identify output binding error
The output binding incorrectly uses "httpTrigger"; it should be "http" with direction "out" for HTTP responses.
Final Answer:
The output binding type should be "http" not "httpTrigger". -> Option A
Quick Check:
HTTP output binding = type "http" [OK]
Hint: HTTP output binding uses type "http", not "httpTrigger" [OK]
Common Mistakes:
Using "httpTrigger" as output binding type incorrectly
Confusing input and output binding types
Setting wrong direction for output binding
5. You want to create an Azure Function with an HTTP trigger that only allows calls with a function key (authLevel set to "function") and responds with JSON containing a greeting message using the "name" query parameter and returns a JSON error message if the "name" query parameter is missing. Which of the following code snippets correctly implements this behavior?
hard
A. import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
name = req.params.get('name')
if not name:
return func.HttpResponse('Missing name', status_code=400)
return func.HttpResponse(f'{"message": "Hello, {name}!"}', mimetype='application/json')
B. import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
name = req.get_json().get('name')
if not name:
return func.HttpResponse('Missing name', status_code=400)
return func.HttpResponse(f'Hello, {name}!')
C. import azure.functions as func
import json
def main(req: func.HttpRequest) -> func.HttpResponse:
name = req.params.get('name')
if not name:
return func.HttpResponse(json.dumps({'error': 'Missing name'}), status_code=400, mimetype='application/json')
return func.HttpResponse(json.dumps({'message': f'Hello, {name}!'}), mimetype='application/json')
D. import azure.functions as func
import json
def main(req: func.HttpRequest) -> func.HttpResponse:
name = req.params.get('name')
return func.HttpResponse(json.dumps({'message': f'Hello, {name}!'}), mimetype='text/plain')
Solution
Step 1: Check authLevel and input handling
authLevel "function" is set in function.json (not shown), so code must handle query param 'name' safely.
Step 2: Validate JSON response and error handling
import azure.functions as func
import json
def main(req: func.HttpRequest) -> func.HttpResponse:
name = req.params.get('name')
if not name:
return func.HttpResponse(json.dumps({'error': 'Missing name'}), status_code=400, mimetype='application/json')
return func.HttpResponse(json.dumps({'message': f'Hello, {name}!'}), mimetype='application/json') uses json.dumps to create proper JSON for both error ({'error': 'Missing name'}) and success ({'message': f'Hello, {name}!'}), with mimetype='application/json' and status_code=400 for errors.
Step 3: Compare other options
A returns plain text error and f-string JSON-like string; C uses get_json() instead of params; D uses text/plain mimetype.
Final Answer:
Uses json.dumps and mimetype='application/json' for both success and error JSON responses. -> Option C
Quick Check:
json.dumps for JSON error and success with application/json mimetype [OK]
Hint: Use json.dumps and mimetype 'application/json' for JSON responses [OK]