0
0
Flaskframework~20 mins

Request object properties in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does request.args contain in Flask?

Consider a Flask route that handles a URL like /search?query=flask&sort=asc. What does request.args represent?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/search')
def search():
    return str(request.args)
AThe raw bytes of the request body
BThe JSON body of the request parsed as a dictionary
CThe form data submitted via POST method
DA dictionary-like object containing the URL query parameters {'query': 'flask', 'sort': 'asc'}
Attempts:
2 left
💡 Hint

Think about what appears after the question mark in a URL.

state_output
intermediate
1:30remaining
What is the output of request.method for a POST request?

Given a Flask route that accepts both GET and POST, what will request.method return when the client sends a POST request?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/submit', methods=['GET', 'POST'])
def submit():
    return request.method
A'post'
B'GET'
C'POST'
DNone
Attempts:
2 left
💡 Hint

HTTP methods are uppercase strings in Flask's request.method.

📝 Syntax
advanced
2:00remaining
Which option correctly accesses JSON data from a Flask request?

Assuming a client sends JSON data {"name": "Alice"} in a POST request, which code correctly extracts the value of name?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/json', methods=['POST'])
def json_route():
    # Extract name from JSON here
    pass
Aname = request.get_json()['name']
Bname = request.json['name']
Cname = request.data['name']
Dname = request.form['name']
Attempts:
2 left
💡 Hint

Use the method that parses JSON safely from the request body.

🔧 Debug
advanced
2:00remaining
Why does accessing request.form['username'] raise a KeyError?

A Flask route expects form data with a field username. The code request.form['username'] raises a KeyError. What is the most likely reason?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/login', methods=['POST'])
def login():
    user = request.form['username']
    return f"Hello {user}"
AThe client did not send form data with a 'username' field
BThe request method is GET, so form data is empty
Crequest.form only works with JSON data
DFlask requires special headers to access form data
Attempts:
2 left
💡 Hint

Think about what happens if the expected form field is missing.

🧠 Conceptual
expert
1:30remaining
Which request property provides the raw bytes of the request body?

In Flask, you want to access the raw bytes sent in the request body regardless of content type. Which request property gives you this?

Arequest.args
Brequest.data
Crequest.form
Drequest.json
Attempts:
2 left
💡 Hint

Think about the raw content before parsing.