Complete the code to define an HTTP triggered Cloud Function in Python.
def hello_world(request): return [1]('Hello, World!')
The function must return a proper HTTP response. make_response creates a valid HTTP response object.
Complete the code to extract a query parameter named 'name' from the HTTP request.
def greet(request): name = request.args.get([1], 'Guest') return f'Hello, {name}!'
The query parameter to extract is named 'name'. Using request.args.get('name') retrieves it.
Fix the error in the code to correctly handle JSON data from the HTTP POST request.
def process_json(request): data = request.[1] return f"Received: {data['message']}"
The request object has a get_json() method that parses the JSON data. Using request.json is also valid but here the correct fix is to use get_json().
Fill both blanks to create a Cloud Function that returns a JSON response with a status code 200.
from flask import jsonify, make_response def json_response(request): data = {'message': 'Success'} response = make_response([1](data), [2]) return response
jsonify converts the dictionary to a JSON response. The status code 200 means success.
Fill all three blanks to create a Cloud Function that reads a JSON field 'name', returns a greeting, and sets content type.
from flask import make_response, jsonify def greet_user(request): data = request.[1] name = data.get([2], 'Guest') response = make_response(jsonify({'greeting': f'Hello, {name}!'})) response.headers['[3]'] = 'application/json' return response
request.args instead of request.jsonrequest.json accesses JSON data. The key to get is 'name'. The header to set content type is 'Content-Type'.