0
0
Flaskframework~10 mins

Why middleware extends functionality in Flask - Test Your Understanding

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

Complete the code to add middleware that logs each request in Flask.

Flask
from flask import Flask, request

app = Flask(__name__)

@app.before_request
def [1]():
    print(f"Request to: {request.path}")

@app.route('/')
def home():
    return 'Hello!'
Drag options to blanks, or click blank then click option'
Ahandle_request
Bbefore_request
Clog_request
Dprocess_request
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that doesn't describe its purpose
Not using the @app.before_request decorator
2fill in blank
medium

Complete the code to create middleware that modifies the response headers.

Flask
from flask import Flask, Response

app = Flask(__name__)

@app.after_request
def add_header(response: Response):
    response.headers['X-Custom-Header'] = [1]
    return response

@app.route('/')
def home():
    return 'Hello!'
Drag options to blanks, or click blank then click option'
A'Middleware-Active'
B'Active-Middleware'
C'Custom-Header'
D'Header-Set'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a header value that doesn't clearly indicate middleware
Forgetting to return the response object
3fill in blank
hard

Fix the error in the middleware function that blocks requests to '/secret'.

Flask
from flask import Flask, request, abort

app = Flask(__name__)

@app.before_request
def block_secret():
    if request.path == [1]:
        abort(403)

@app.route('/secret')
def secret():
    return 'Top secret!'
Drag options to blanks, or click blank then click option'
A'/secret'
B'secret/'
C'/Secret'
D'secret'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a path without the leading slash
Using incorrect casing in the path string
4fill in blank
hard

Fill both blanks to create middleware that measures request processing time and adds it to response headers.

Flask
import time
from flask import Flask, g, request

app = Flask(__name__)

@app.before_request
def start_timer():
    g.start = [1]

@app.after_request
def add_duration(response):
    duration = time.time() - g.start
    response.headers['X-Duration'] = str(duration) + [2]
    return response
Drag options to blanks, or click blank then click option'
Atime.time()
B' seconds'
C' ms'
Dtime.perf_counter()
Attempts:
3 left
💡 Hint
Common Mistakes
Using time.perf_counter() which is more precise but less common here
Forgetting to convert duration to string before concatenation
5fill in blank
hard

Fill all three blanks to create middleware that modifies request data and response data.

Flask
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.before_request
def modify_request():
    if request.is_json:
        data = request.get_json()
        data[[1]] = [2]
        request.json = data

@app.after_request
def modify_response(response):
    if response.is_json:
        json_data = response.get_json()
        json_data[[3]] = 'modified by middleware'
        response.set_data(jsonify(json_data).get_data())
    return response
Drag options to blanks, or click blank then click option'
A'middleware_flag'
BTrue
C'middleware_status'
D'status'
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys that don't match between request and response
Not updating the response data properly after modification