0
0
Flaskframework~10 mins

Setting and reading cookies in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Setting and reading cookies
Client sends request
Server reads cookies from request
Server processes request
Server sets cookies in response
Response sent back to client with Set-Cookie header
Client stores cookies
Next request includes stored cookies
This flow shows how a client sends a request with cookies, the server reads them, processes the request, sets new cookies in the response, and the client stores them for future requests.
Execution Sample
Flask
from flask import Flask, request, make_response
app = Flask(__name__)

@app.route('/')
def index():
    user = request.cookies.get('user')
    resp = make_response(f'Hello {user}')
    resp.set_cookie('user', 'Alice')
    return resp
This Flask code reads a cookie named 'user' from the request and sets a cookie 'user' with value 'Alice' in the response.
Execution Table
StepActionRequest CookiesRead Cookie 'user'Set Cookie 'user'Response Content
1Client sends first request{}NoneSet 'user'='Alice'Hello None
2Client stores cookie 'user'='Alice'{}N/AN/AN/A
3Client sends second request with cookie{'user': 'Alice'}AliceSet 'user'='Alice'Hello Alice
💡 Execution stops after response is sent and client stores cookie for next requests.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
request.cookies{}{}{}{'user': 'Alice'}
userNoneNoneNoneAlice
response cookie 'user'NoneAliceAliceAlice
Key Moments - 3 Insights
Why is 'user' None in the first response content?
Because the client did not send any 'user' cookie in the first request (see execution_table step 1), so request.cookies.get('user') returns None.
How does the cookie 'user' get sent back to the server in the second request?
After the first response, the client stores the cookie 'user=Alice' (execution_table step 2). The browser automatically includes this cookie in the next request headers (step 3).
Why does the server set the cookie 'user'='Alice' on every response?
The code calls resp.set_cookie('user', 'Alice') every time the route runs, so the server instructs the client to keep the cookie updated each response.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'user' read from cookies at step 1?
A'' (empty string)
B'Alice'
CNone
DError
💡 Hint
Check the 'Read Cookie user' column at step 1 in execution_table.
At which step does the client start sending the 'user' cookie back to the server?
AStep 3
BStep 2
CStep 1
DNever
💡 Hint
Look at the 'Request Cookies' column in execution_table to see when 'user' appears.
If the server did not call set_cookie, what would happen to the 'user' cookie in the client?
AIt would be deleted immediately
BIt would remain stored and sent back
CIt would never be stored
DIt would cause an error
💡 Hint
Cookies stay stored on the client until expired or deleted; server setting cookie updates or removes it.
Concept Snapshot
Flask cookies flow:
- Read cookies from request: request.cookies.get('name')
- Set cookies in response: resp.set_cookie('name', 'value')
- Client stores cookies and sends them in future requests
- Server can update cookies each response
- Cookies enable state across requests
Full Transcript
In Flask, cookies are small pieces of data stored on the client browser. When a client sends a request, the server can read cookies from request.cookies. The server can send cookies back by setting them in the response using resp.set_cookie. The client stores these cookies and includes them in future requests automatically. This allows the server to remember information about the client between requests. In the example, the server reads a cookie named 'user' and sets it to 'Alice' in every response. The first request has no cookies, so 'user' is None. After the first response, the client stores the cookie and sends it back in the next request, so the server reads 'Alice' as the user cookie.