0
0
Flaskframework~10 mins

Request validation in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
ARequest
Brender_template
CFlask
Djsonify
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase 'flask' instead of 'Flask'
2fill in blank
medium

Complete 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'
Ajson
Bform
Cheaders
Dargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.args which is for URL parameters
Using request.form which is for form data
3fill in blank
hard

Fix 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'
Aemail
Busername
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for a wrong key like 'username' or 'email'
Not checking if data is None before key check
4fill in blank
hard

Fill 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'
A'age'
Bint
Cstr
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key like 'name' instead of 'age'
Checking type against 'str' instead of 'int'
5fill in blank
hard

Fill 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'
A'error'
B'Missing email'
C400
D'message'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong JSON key like 'message' instead of 'error'
Returning status 200 instead of 400