Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask module correctly.
Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'request' instead of 'Flask'
Using 'jsonify' here instead of 'Flask'
✗ Incorrect
The Flask class is imported from the flask module to create the app instance.
2fill in blank
mediumComplete the code to access JSON data from a POST request.
Flask
data = [1].get_json() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'json.get_json()' which is incorrect
Trying to call get_json() on 'app' or 'Flask'
✗ Incorrect
The request object provides access to the incoming request data, including JSON.
3fill in blank
hardFix the error in the route decorator to accept POST requests.
Flask
@app.route('/submit', methods=[[1]])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'GET' which is default but does not accept JSON body
Using 'PUT' or 'DELETE' which are different HTTP methods
✗ Incorrect
The route must specify 'POST' in the methods list to accept POST requests.
4fill in blank
hardFill both blanks to return a JSON response with a message and status code 200.
Flask
return [1]({'message': 'Success'}), [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a plain dict without jsonify
Using status code 201 which means created
✗ Incorrect
jsonify creates a JSON response and 200 is the HTTP status code for success.
5fill in blank
hardFill all three blanks to parse JSON, extract 'name', and return a greeting message.
Flask
data = [1].get_json() name = data.get([2], 'Guest') return [3]({'greeting': f'Hello, {name}!'})
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'app' instead of 'request'
Not using jsonify to return JSON response
✗ Incorrect
Use request.get_json() to get JSON data, extract the 'name' key, and use jsonify to return JSON response.