Complete the code to register an after_request hook in Flask.
from flask import Flask app = Flask(__name__) @app.[1] def add_header(response): response.headers['X-Custom-Header'] = 'Value' return response
The @app.after_request decorator registers a function to run after each request to modify the response.
Complete the code to add a header 'X-Powered-By' with value 'Flask' in the after_request hook.
from flask import Flask app = Flask(__name__) @app.after_request def add_powered_by(response): response.headers['[1]'] = 'Flask' return response
The header X-Powered-By is commonly used to indicate the technology powering the server.
Fix the error in the after_request hook to ensure the response is returned properly.
from flask import Flask app = Flask(__name__) @app.after_request def fix_response(response): response.headers['Cache-Control'] = 'no-store' [1]
The after_request function must return the response object to send it back to the client.
Fill both blanks to create an after_request hook that adds a header and logs the response status.
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
The header X-Processed is custom, and response.status_code gives the HTTP status code.
Fill all three blanks to create an after_request hook that adds two headers and modifies the response body length check.
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
The headers X-Feature-1 and X-Feature-2 are custom. The condition checks if the response data length is greater than 1000 bytes.