0
0
GCPcloud~10 mins

HTTP triggered functions in GCP - 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 triggered Cloud Function in Python.

GCP
def hello_world(request):
    return [1]('Hello, World!')
Drag options to blanks, or click blank then click option'
AResponse
Bstr
Cmake_response
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of returning a response
Returning a plain string without wrapping in a response
2fill in blank
medium

Complete the code to extract a query parameter named 'name' from the HTTP request.

GCP
def greet(request):
    name = request.args.get([1], 'Guest')
    return f'Hello, {name}!'
Drag options to blanks, or click blank then click option'
A'user'
B'name'
C'id'
D'guest'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter name
Forgetting to provide a default value
3fill in blank
hard

Fix the error in the code to correctly handle JSON data from the HTTP POST request.

GCP
def process_json(request):
    data = request.[1]
    return f"Received: {data['message']}"
Drag options to blanks, or click blank then click option'
Aget_json
Bjson
Cjson_data
Djson()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling get_json() on request.json which is already parsed
Using incorrect attribute names
4fill in blank
hard

Fill both blanks to create a Cloud Function that returns a JSON response with a status code 200.

GCP
from flask import jsonify, make_response

def json_response(request):
    data = {'message': 'Success'}
    response = make_response([1](data), [2])
    return response
Drag options to blanks, or click blank then click option'
Ajsonify
B404
C200
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status code
Not converting data to JSON properly
5fill in blank
hard

Fill all three blanks to create a Cloud Function that reads a JSON field 'name', returns a greeting, and sets content type.

GCP
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
Drag options to blanks, or click blank then click option'
Ajson
B'name'
CContent-Type
Dargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.args instead of request.json
Forgetting to set the content type header
Using wrong key names