Cookies help websites remember you by storing small pieces of information on your browser. This makes your experience smoother and personalized.
How cookies work in HTTP in Flask
from flask import Flask, request, make_response app = Flask(__name__) @app.route('/') def index(): username = request.cookies.get('username') if username: return f'Hello, {username}!' else: resp = make_response('Hello, Guest!') resp.set_cookie('username', 'Alice') return resp
Use request.cookies.get('cookie_name') to read a cookie sent by the browser.
Use response.set_cookie('cookie_name', 'value') to send a cookie to the browser.
theme with the value dark in the user's browser.resp.set_cookie('theme', 'dark')
username cookie sent by the browser, if it exists.username = request.cookies.get('username')username cookie from the browser.resp.delete_cookie('username')This Flask app checks if the browser sent a cookie named username. If yes, it greets the user by name. If not, it sets the cookie with the value Alice and welcomes the new visitor.
from flask import Flask, request, make_response app = Flask(__name__) @app.route('/') def index(): username = request.cookies.get('username') if username: return f'Welcome back, {username}!' else: resp = make_response('Hello, new visitor! Setting your cookie now.') resp.set_cookie('username', 'Alice') return resp if __name__ == '__main__': app.run(debug=True)
Cookies are stored on the user's browser and sent with every request to the server for that domain.
Cookies can have expiration times, making them temporary or persistent.
Always be careful with sensitive data; do not store passwords in cookies.
Cookies store small data on the browser to remember users.
Flask uses request.cookies to read and response.set_cookie() to write cookies.
Cookies improve user experience by saving preferences and login info.
Practice
Solution
Step 1: Understand what cookies do in HTTP
Cookies store small data on the user's browser to remember information between requests.Step 2: Identify Flask's use of cookies
Flask uses cookies to save user preferences or login info on the client side.Final Answer:
To store small pieces of data on the user's browser for later use -> Option AQuick Check:
Cookies store data on browser = D [OK]
- Thinking cookies speed up the server
- Confusing cookies with encryption
- Assuming cookies manage databases
Solution
Step 1: Recall Flask's cookie setting method
Cookies are set on the response object, not the request.Step 2: Identify the correct method
The method to set a cookie isset_cookie()on the response object.Final Answer:
response.set_cookie() -> Option CQuick Check:
Set cookie on response = C [OK]
- Using request.set_cookie() instead of response.set_cookie()
- Confusing request.cookies() as a setter
- Using response.cookies() which does not exist
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
user = request.cookies.get('user')
return f'Hello, {user}!'Solution
Step 1: Understand how Flask reads cookies
The code usesrequest.cookies.get('user')to get the cookie value named 'user'.Step 2: Check the cookie value sent by client
The client sends a cookie 'user' with value 'Alice', souservariable will be 'Alice'.Final Answer:
Hello, Alice! -> Option AQuick Check:
Cookie 'user' = Alice, output = A [OK]
- Assuming cookie value is None if present
- Forgetting to use .get() method
- Confusing cookie name with literal string
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
response = 'Hello'
response.set_cookie('theme', 'dark')
return responseSolution
Step 1: Identify the response object type
The variableresponseis a string, not a Flask response object.Step 2: Understand how to set cookies in Flask
Cookies can only be set on a response object, like one created bymake_response(), not on a plain string.Final Answer:
You cannot set cookies on a string response object -> Option BQuick Check:
Set cookies on response object, not string = B [OK]
- Trying to set cookies on string instead of response object
- Thinking cookie names must be special
- Believing set_cookie needs extra arguments always
Solution
Step 1: Identify how to create a response object
Usemake_response()to create a response object to set cookies.Step 2: Set cookie with expiration
Useset_cookie()withmax_age=86400(seconds in 1 day) to set expiration.Step 3: Check each option
from flask import make_response @app.route('/') def index(): resp = make_response('Welcome') resp.set_cookie('visited', 'yes', max_age=86400) return resp correctly usesmake_responseandset_cookiewithmax_age. Others either set cookie on string or request or use wrong syntax.Final Answer:
resp.set_cookie('visited', 'yes', max_age=86400) -> Option DQuick Check:
Use make_response + set_cookie + max_age = A [OK]
- Setting cookie on string instead of response object
- Trying to set cookie on request object
- Using resp.cookies dict instead of set_cookie method
Ready to prove it?
Pass the test and this pattern moves unknown
