0
0
Flaskframework~10 mins

After_request hooks 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 register an after_request hook in Flask.

Flask
from flask import Flask
app = Flask(__name__)

@app.[1]
def add_header(response):
    response.headers['X-Custom-Header'] = 'Value'
    return response
Drag options to blanks, or click blank then click option'
Abefore_request
Bafter_request
Croute
Derrorhandler
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.before_request instead of @app.after_request
Forgetting to return the response object
2fill in blank
medium

Complete the code to add a header 'X-Powered-By' with value 'Flask' in the after_request hook.

Flask
from flask import Flask
app = Flask(__name__)

@app.after_request
def add_powered_by(response):
    response.headers['[1]'] = 'Flask'
    return response
Drag options to blanks, or click blank then click option'
AX-Powered-By
BContent-Type
CServer
DAuthorization
Attempts:
3 left
💡 Hint
Common Mistakes
Using standard headers like Content-Type instead of a custom header
Misspelling the header name
3fill in blank
hard

Fix the error in the after_request hook to ensure the response is returned properly.

Flask
from flask import Flask
app = Flask(__name__)

@app.after_request
def fix_response(response):
    response.headers['Cache-Control'] = 'no-store'
    [1]
Drag options to blanks, or click blank then click option'
Aresponse
Bprint(response)
Creturn response
Dpass
Attempts:
3 left
💡 Hint
Common Mistakes
Not returning the response object
Using print instead of return
4fill in blank
hard

Fill both blanks to create an after_request hook that adds a header and logs the response status.

Flask
from flask import Flask
app = Flask(__name__)

@app.after_request
def log_and_header(response):
    response.headers['[1]'] = 'True'
    print('Response status:', response.[2])
    return response
Drag options to blanks, or click blank then click option'
AX-Processed
Bstatus_code
Ccontent_length
DServer
Attempts:
3 left
💡 Hint
Common Mistakes
Using standard headers instead of custom ones
Accessing response.status instead of response.status_code
5fill in blank
hard

Fill all three blanks to create an after_request hook that adds two headers and modifies the response body length check.

Flask
from flask import Flask
app = Flask(__name__)

@app.after_request
def modify_response(response):
    response.headers['[1]'] = 'Yes'
    response.headers['[2]'] = 'No'
    if len(response.get_data()) [3] 1000:
        print('Large response')
    return response
Drag options to blanks, or click blank then click option'
AX-Feature-1
BX-Feature-2
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using standard header names
Using wrong comparison operator like '<' instead of '>'