0
0
Azurecloud~10 mins

Functions with HTTP triggers in Azure - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an HTTP trigger in an Azure Function.

Azure
def main(req: func.HttpRequest) -> func.HttpResponse:
    if req.method == [1]:
        return func.HttpResponse("Hello, world!")
Drag options to blanks, or click blank then click option'
A"POST"
B"PUT"
C"DELETE"
D"GET"
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for simple retrieval.
2fill in blank
medium

Complete the code to return a JSON response from the Azure Function.

Azure
import json

def main(req: func.HttpRequest) -> func.HttpResponse:
    data = {"message": "Hello"}
    return func.HttpResponse(json.dumps([1]), mimetype="application/json")
Drag options to blanks, or click blank then click option'
Adata
Breq
Cfunc
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the request object instead of data.
3fill in blank
hard

Fix the error in the function signature for an HTTP triggered Azure Function.

Azure
def main([1]) -> func.HttpResponse:
    return func.HttpResponse("OK")
Drag options to blanks, or click blank then click option'
Areq
Breq: func.HttpRequest
Crequest
Drequest: HttpRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Missing type annotation or wrong parameter name.
4fill in blank
hard

Fill both blanks to correctly read a query parameter and return it in the response.

Azure
def main(req: func.HttpRequest) -> func.HttpResponse:
    name = req.params.get([1])
    if not name:
        return func.HttpResponse("Name not found", status_code=[2])
    return func.HttpResponse(f"Hello, {name}!")
Drag options to blanks, or click blank then click option'
A"name"
B400
C404
D"user"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter key or incorrect status code.
5fill in blank
hard

Fill all three blanks to parse JSON body and return a field value in the response.

Azure
import json

def main(req: func.HttpRequest) -> func.HttpResponse:
    try:
        req_body = req.get_json()
    except ValueError:
        return func.HttpResponse("Invalid JSON", status_code=[1])
    name = req_body.get([2])
    if not name:
        return func.HttpResponse("Missing name", status_code=[3])
    return func.HttpResponse(f"Hello, {name}!")
Drag options to blanks, or click blank then click option'
A400
B"name"
C422
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status codes or keys for JSON parsing.