Complete the code to define an HTTP trigger in an Azure Function.
def main(req: func.HttpRequest) -> func.HttpResponse: if req.method == [1]: return func.HttpResponse("Hello, world!")
The HTTP trigger checks if the request method is "GET" to respond accordingly.
Complete the code to return a JSON response from the Azure Function.
import json def main(req: func.HttpRequest) -> func.HttpResponse: data = {"message": "Hello"} return func.HttpResponse(json.dumps([1]), mimetype="application/json")
The data dictionary is converted to JSON string to send as response.
Fix the error in the function signature for an HTTP triggered Azure Function.
def main([1]) -> func.HttpResponse: return func.HttpResponse("OK")
The function parameter must be typed as func.HttpRequest for HTTP triggers.
Fill both blanks to correctly read a query parameter and return it in the response.
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}!")
The function looks for the 'name' query parameter and returns 400 if missing.
Fill all three blanks to parse JSON body and return a field value in the response.
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}!")
400 is used for invalid JSON, 'name' is the key to get, and 404 for missing name.