0
0
Flaskframework~10 mins

How cookies work in HTTP in Flask - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How cookies work in HTTP
Client sends HTTP request
Server checks for cookies
Server sends HTTP response with Set-Cookie header
Client stores cookie
Next client request includes Cookie header
Server reads cookie and customizes response
This flow shows how a client and server exchange cookies via HTTP headers to remember information across requests.
Execution Sample
Flask
from flask import Flask, request, make_response
app = Flask(__name__)

@app.route('/')
def index():
    username = request.cookies.get('username')
    resp = make_response(f'Hello {username}')
    resp.set_cookie('username', 'Alice')
    return resp
This Flask code sets a cookie named 'username' and reads it on subsequent requests to greet the user.
Execution Table
StepActionRequest HeadersResponse HeadersCookie StoredOutput
1Client sends first requestNo Cookie headerSet-Cookie: username=Aliceusername=AliceHello None
2Client sends second requestCookie: username=AliceSet-Cookie: username=Aliceusername=AliceHello Alice
3Client sends third requestCookie: username=AliceSet-Cookie: username=Aliceusername=AliceHello Alice
4No more requestsN/AN/Ausername=AliceSession continues with cookie
💡 Execution stops when client stops sending requests or closes session.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
username cookieNoneAliceAliceAliceAlice
request.cookies.get('username')NoneNoneAliceAliceAlice
Key Moments - 3 Insights
Why does the first response greet with 'Hello None'?
At step 1 in the execution_table, the client has not sent any cookie yet, so request.cookies.get('username') returns None.
How does the client remember the cookie for the next request?
After step 1, the response includes 'Set-Cookie: username=Alice', so the client stores this cookie and sends it in the next request header as shown in step 2.
Why does the server keep sending Set-Cookie header on every response?
In this example, the server sets the cookie on every response to refresh or update it, as seen in response headers at steps 1, 2, and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'username' cookie after step 1?
ABob
BNone
CAlice
DEmpty string
💡 Hint
Check the 'Cookie Stored' column at step 1 in the execution_table.
At which step does the client start sending the cookie back to the server?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Request Headers' column to see when 'Cookie: username=Alice' appears.
If the server did not send 'Set-Cookie' header in the response, what would happen to the cookie?
AClient would keep sending the old cookie
BClient would delete the cookie immediately
CClient would never send any cookie
DServer would receive empty cookie
💡 Hint
Refer to how cookies persist on the client side unless expired or deleted.
Concept Snapshot
HTTP cookies are small data sent by server in 'Set-Cookie' header.
Client stores cookies and sends them back in 'Cookie' header.
Server reads cookies to remember user info across requests.
In Flask, use request.cookies to read and response.set_cookie to set cookies.
Cookies enable session-like behavior over stateless HTTP.
Full Transcript
This visual execution shows how HTTP cookies work between a client and a Flask server. Initially, the client sends a request without cookies. The server responds with a 'Set-Cookie' header to store a cookie named 'username' with value 'Alice'. The client saves this cookie and includes it in the 'Cookie' header of the next request. The server reads this cookie and uses it to personalize the response. This cycle repeats, demonstrating how cookies help maintain state in HTTP. The execution table tracks headers and cookie values step-by-step, while the variable tracker shows how the cookie value changes from None to 'Alice'. Key moments clarify why the first greeting lacks a username and how cookies persist. The quiz tests understanding of cookie storage and transmission. This example uses Flask's request.cookies and response.set_cookie methods to handle cookies simply and clearly.