Response headers are pieces of information sent by the server to the client along with the response. They describe details like content type, caching, and cookies.
Click to reveal answer
beginner
How do you set a custom response header in Flask?
You can set a custom response header by modifying the response object. For example: response = make_response('Hello') response.headers['X-Custom-Header'] = 'Value'
Click to reveal answer
beginner
What Flask function helps create a response object to modify headers?
The make_response() function creates a response object that you can modify, including adding or changing headers before sending it back.
Click to reveal answer
intermediate
Why are response headers important in web applications?
Response headers control how browsers handle the response, like caching, content type, security policies, and cookies. They help improve user experience and security.
Click to reveal answer
intermediate
How can you add multiple headers to a Flask response?
You can add multiple headers by setting them one by one on the response.headers dictionary or by passing a dictionary of headers when creating the response.
Click to reveal answer
Which Flask function is used to create a response object for setting headers?
Asend_file()
Brender_template()
Crequest.get()
Dmake_response()
✗ Incorrect
make_response() creates a response object that you can modify, including headers.
How do you add a custom header 'X-Test' with value '123' to a Flask response?
Aresponse.headers['X-Test'] = '123'
Brequest.headers['X-Test'] = '123'
Capp.headers['X-Test'] = '123'
Dflask.headers['X-Test'] = '123'
✗ Incorrect
Headers are set on the response object, not on request or app.
What does the 'Content-Type' response header specify?
AThe server's IP address
BThe type of data sent in the response
CThe size of the response
DThe client's browser type
✗ Incorrect
Content-Type tells the browser what kind of data it is receiving.
Which of these is NOT a typical use of response headers?
AChanging the URL of the request
BControlling caching behavior
CSetting cookies
DSpecifying security policies
✗ Incorrect
Response headers do not change the request URL.
If you want to add multiple headers at once, which approach is correct?
ASet headers in the HTML body
BModify the request headers
CPass a dictionary of headers when creating the response
DUse app.config to set headers
✗ Incorrect
You can pass a dictionary of headers to make_response or set them one by one on the response.headers.
Explain how to add a custom response header in Flask and why you might want to do that.
Think about how you create and modify the response object.
You got /4 concepts.
Describe the role of response headers in web communication and give two examples of common headers.
Consider what information the server sends to the browser besides the page content.
You got /3 concepts.
Practice
(1/5)
1. What is the purpose of response headers in a Flask web application?
easy
A. To send extra information from the server to the browser
B. To store user data on the server
C. To write data into the database
D. To create HTML content dynamically
Solution
Step 1: Understand what response headers do
Response headers carry extra information about the response, like content type or caching rules.
Step 2: Identify their role in Flask
In Flask, response headers are sent from the server to the browser to control how the browser handles the response.
Final Answer:
To send extra information from the server to the browser -> Option A
Quick Check:
Response headers = extra info sent to browser [OK]
Hint: Response headers send info from server to browser [OK]
Common Mistakes:
Confusing headers with server-side data storage
Thinking headers create HTML content
Mixing headers with database operations
2. Which of the following is the correct way to add a custom header X-Custom-Header with value Test to a Flask response?
easy
A. response.set_header('X-Custom-Header', 'Test')
B. response.add_header('X-Custom-Header', 'Test')
C. response.header('X-Custom-Header', 'Test')
D. response.headers['X-Custom-Header'] = 'Test'
Solution
Step 1: Recall Flask response header syntax
In Flask, headers are set by assigning values to keys in the response.headers dictionary.
Step 2: Match the correct syntax
Only response.headers['X-Custom-Header'] = 'Test' uses the correct dictionary assignment syntax.
Final Answer:
response.headers['X-Custom-Header'] = 'Test' -> Option D
Quick Check:
Use dictionary assignment for headers [OK]
Hint: Set headers by assigning to response.headers dict [OK]
Common Mistakes:
Using non-existent methods like add_header or set_header
Calling response.header() which does not exist
Trying to set headers as function calls
3. Given this Flask code snippet, what will be the value of the Content-Type header in the response?