Challenge - 5 Problems
Response Headers Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask response header code?
Consider this Flask route that sets a custom response header. What will the client receive in the response headers?
Flask
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('Hello World') response.headers['X-Custom-Header'] = 'MyValue' return response
Attempts:
2 left
💡 Hint
Think about how Flask's make_response allows modifying headers before sending.
✗ Incorrect
Using make_response, you can add or modify headers before returning the response. Here, 'X-Custom-Header' is set to 'MyValue', so the client receives it.
📝 Syntax
intermediate2:00remaining
Which option correctly sets multiple headers in a Flask response?
You want to set two headers: 'Cache-Control' to 'no-cache' and 'X-Powered-By' to 'Flask'. Which code snippet does this correctly?
Flask
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('Hello') # Set headers here return response
Attempts:
2 left
💡 Hint
Headers behave like a dictionary in Flask response objects.
✗ Incorrect
The headers attribute is a dictionary-like object. You can assign values by key. Option A replaces the entire headers object which is not allowed. Option A and D use non-existent methods.
🔧 Debug
advanced2:00remaining
Why does this Flask code fail to send the custom header?
Look at this Flask route. Why does the client not receive the 'X-Test' header?
Flask
from flask import Flask app = Flask(__name__) @app.route('/') def index(): response = 'Hello' response.headers['X-Test'] = '123' return response
Attempts:
2 left
💡 Hint
Check the type of the response variable before setting headers.
✗ Incorrect
The variable 'response' is a plain string. Strings do not have a headers attribute. You must create a response object (e.g., with make_response) to set headers.
❓ state_output
advanced2:00remaining
What is the value of the 'Content-Type' header after this Flask route runs?
Given this Flask route, what will be the 'Content-Type' header in the response?
Flask
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def index(): response = make_response('<h1>Hi</h1>') response.headers['Content-Type'] = 'application/json' return response
Attempts:
2 left
💡 Hint
Headers set explicitly override defaults.
✗ Incorrect
The code explicitly sets 'Content-Type' to 'application/json', so this header value is sent to the client.
🧠 Conceptual
expert2:00remaining
Which statement about Flask response headers is true?
Choose the correct statement about how Flask handles response headers.
Attempts:
2 left
💡 Hint
Think about when the response is finalized and sent.
✗ Incorrect
Headers must be set before returning the response object. After returning, the response is sent and headers cannot be changed. Other options are incorrect because Flask does not merge duplicate headers automatically, headers are respected regardless of body type if response object is used, and no special decorator is needed.