0
0
Flaskframework~20 mins

Response headers in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Headers Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
AThe response includes a header 'X-Custom-Header' with value 'Hello World'.
BThe response includes a header 'X-Custom-Header' with value 'MyValue'.
CThe response has no custom headers; only default headers are sent.
DThe code raises a runtime error because headers cannot be set this way.
Attempts:
2 left
💡 Hint
Think about how Flask's make_response allows modifying headers before sending.
📝 Syntax
intermediate
2: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
A
response.headers['Cache-Control'] = 'no-cache'
response.headers['X-Powered-By'] = 'Flask'
B
response.headers.add('Cache-Control', 'no-cache')
response.headers.add('X-Powered-By', 'Flask')
Cresponse.headers = {'Cache-Control': 'no-cache', 'X-Powered-By': 'Flask'}
D
response.set_header('Cache-Control', 'no-cache')
response.set_header('X-Powered-By', 'Flask')
Attempts:
2 left
💡 Hint
Headers behave like a dictionary in Flask response objects.
🔧 Debug
advanced
2: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
ABecause the header name 'X-Test' is reserved and ignored by Flask.
BBecause Flask automatically strips custom headers unless explicitly allowed.
CBecause headers must be set before returning the string, using a decorator.
DBecause 'response' is a string, not a response object, so it has no headers attribute.
Attempts:
2 left
💡 Hint
Check the type of the response variable before setting headers.
state_output
advanced
2: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
A'application/json'
B'text/html; charset=utf-8'
C'text/plain'
DNo 'Content-Type' header is set
Attempts:
2 left
💡 Hint
Headers set explicitly override defaults.
🧠 Conceptual
expert
2:00remaining
Which statement about Flask response headers is true?
Choose the correct statement about how Flask handles response headers.
AHeaders set on the response object are ignored if the response body is a string.
BFlask automatically merges duplicate headers with the same name into a single header.
CFlask response headers can be modified only before the response is returned, not after.
DFlask requires headers to be set using a special decorator, not directly on the response.
Attempts:
2 left
💡 Hint
Think about when the response is finalized and sent.