Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask class.
Flask
from flask import [1] app = [1](__name__)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase 'flask' 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 get JSON data from a POST request.
Flask
from flask import request @app.route('/data', methods=['POST']) def data(): data = request.[1] return 'Received'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.args which is for URL parameters
Using request.form which is for form data
✗ Incorrect
Use request.json to access JSON data sent in the request body.
3fill in blank
hardFix the error in the code to validate a required JSON field 'name'.
Flask
from flask import request, abort @app.route('/submit', methods=['POST']) def submit(): data = request.json if not data or '[1]' not in data: abort(400, 'Missing name') return 'OK'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for a wrong key like 'username' or 'email'
Not checking if data is None before key check
✗ Incorrect
The code checks if the key 'name' is present in the JSON data to validate the request.
4fill in blank
hardFill both blanks to extract 'age' from JSON and check if it is an integer.
Flask
data = request.json age = data.get([1]) if not isinstance(age, [2]): abort(400, 'Age must be an integer')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key like 'name' instead of 'age'
Checking type against 'str' instead of 'int'
✗ Incorrect
Use data.get('age') to get the age value and check if it is an int.
5fill in blank
hardFill all three blanks to return a JSON error response with status 400.
Flask
from flask import jsonify, abort @app.route('/check', methods=['POST']) def check(): data = request.json if not data or 'email' not in data: return jsonify([1]: [2]), [3] return 'Valid'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong JSON key like 'message' instead of 'error'
Returning status 200 instead of 400
✗ Incorrect
Return a JSON object with key 'error' and message 'Missing email' and HTTP status 400.