Bird
Raised Fist0
Flaskframework~10 mins

How cookies work in HTTP in Flask - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a cookie named 'user' with value 'Alice' in a Flask response.

Flask
from flask import Flask, make_response
app = Flask(__name__)

@app.route('/')
def index():
    resp = make_response('Hello World')
    resp.set_cookie('[1]', 'Alice')
    return resp
Drag options to blanks, or click blank then click option'
Auser
Bsession
Cusername
Dcookie
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong cookie name like 'session' or 'cookie' instead of 'user'.
Forgetting to call set_cookie on the response object.
2fill in blank
medium

Complete the code to read the cookie named 'user' from the Flask request.

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/')
def index():
    user = request.cookies.get('[1]')
    return f'User is {user}'
Drag options to blanks, or click blank then click option'
Ausername
Bsession
Cuser
Dcookie
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different cookie name than the one set.
Trying to access cookies directly without using request.cookies.get().
3fill in blank
hard

Fix the error in the code to delete the cookie named 'user' in the Flask response.

Flask
from flask import Flask, make_response
app = Flask(__name__)

@app.route('/logout')
def logout():
    resp = make_response('Logged out')
    resp.[1]('user')
    return resp
Drag options to blanks, or click blank then click option'
Adelete_cookie
Bset_cookie
Cclear_cookie
Dremove_cookie
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like 'remove_cookie'.
Trying to delete cookie by setting it to None without proper method.
4fill in blank
hard

Fill both blanks to create a cookie named 'session_id' with value 'abc123' that expires in 1 day.

Flask
from flask import Flask, make_response
from datetime import timedelta
app = Flask(__name__)

@app.route('/set')
def set_cookie():
    resp = make_response('Cookie set')
    resp.set_cookie('[1]', '[2]', max_age=86400)
    return resp
Drag options to blanks, or click blank then click option'
Asession_id
Babc123
Cuser
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong cookie names or values.
Not setting max_age to control cookie expiration.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps cookie names to their values for cookies with names starting with 'sess'.

Flask
cookies = [1]: [2] for [3] in request.cookies if [3].startswith('sess')}
Drag options to blanks, or click blank then click option'
Aname
Brequest.cookies[name]
Dcookie
Frequest.cookies[cookie]
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names in the comprehension.
Trying to use the cookie value as the loop variable.

Practice

(1/5)
1. What is the main purpose of cookies in HTTP when using Flask?
easy
A. To store small pieces of data on the user's browser for later use
B. To increase the speed of the Flask server
C. To encrypt user data on the server
D. To manage database connections automatically

Solution

  1. Step 1: Understand what cookies do in HTTP

    Cookies store small data on the user's browser to remember information between requests.
  2. Step 2: Identify Flask's use of cookies

    Flask uses cookies to save user preferences or login info on the client side.
  3. Final Answer:

    To store small pieces of data on the user's browser for later use -> Option A
  4. Quick Check:

    Cookies store data on browser = D [OK]
Hint: Cookies save small data on browser, not server speed [OK]
Common Mistakes:
  • Thinking cookies speed up the server
  • Confusing cookies with encryption
  • Assuming cookies manage databases
2. Which Flask method is used to set a cookie in the HTTP response?
easy
A. request.set_cookie()
B. request.cookies()
C. response.set_cookie()
D. response.cookies()

Solution

  1. Step 1: Recall Flask's cookie setting method

    Cookies are set on the response object, not the request.
  2. Step 2: Identify the correct method

    The method to set a cookie is set_cookie() on the response object.
  3. Final Answer:

    response.set_cookie() -> Option C
  4. Quick Check:

    Set cookie on response = C [OK]
Hint: Set cookies on response, read from request [OK]
Common Mistakes:
  • Using request.set_cookie() instead of response.set_cookie()
  • Confusing request.cookies() as a setter
  • Using response.cookies() which does not exist
3. Given this Flask code snippet, what will be printed when the client sends a cookie named 'user' with value 'Alice'?
from flask import Flask, request
app = Flask(__name__)

@app.route('/')
def index():
    user = request.cookies.get('user')
    return f'Hello, {user}!'
medium
A. Hello, Alice!
B. Hello, !
C. Hello, None!
D. Hello, user!

Solution

  1. Step 1: Understand how Flask reads cookies

    The code uses request.cookies.get('user') to get the cookie value named 'user'.
  2. Step 2: Check the cookie value sent by client

    The client sends a cookie 'user' with value 'Alice', so user variable will be 'Alice'.
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    Cookie 'user' = Alice, output = A [OK]
Hint: request.cookies.get('name') returns cookie value or None [OK]
Common Mistakes:
  • Assuming cookie value is None if present
  • Forgetting to use .get() method
  • Confusing cookie name with literal string
4. What is wrong with this Flask code that tries to set a cookie?
from flask import Flask, request
app = Flask(__name__)

@app.route('/')
def index():
    response = 'Hello'
    response.set_cookie('theme', 'dark')
    return response
medium
A. The cookie name 'theme' is invalid
B. You cannot set cookies on a string response object
C. The set_cookie method requires a third argument
D. Flask does not support cookies

Solution

  1. Step 1: Identify the response object type

    The variable response is a string, not a Flask response object.
  2. Step 2: Understand how to set cookies in Flask

    Cookies can only be set on a response object, like one created by make_response(), not on a plain string.
  3. Final Answer:

    You cannot set cookies on a string response object -> Option B
  4. Quick Check:

    Set cookies on response object, not string = B [OK]
Hint: Use make_response() to set cookies, not plain strings [OK]
Common Mistakes:
  • Trying to set cookies on string instead of response object
  • Thinking cookie names must be special
  • Believing set_cookie needs extra arguments always
5. You want to create a Flask route that sets a cookie named 'visited' with value 'yes' that expires in 1 day. Which code correctly does this?
hard
A. from flask import make_response @app.route('/') def index(): resp = make_response('Welcome') resp.cookies['visited'] = 'yes' return resp
B. @app.route('/') def index(): response = 'Welcome' response.set_cookie('visited', 'yes', max_age=86400) return response
C. from flask import request @app.route('/') def index(): request.set_cookie('visited', 'yes', max_age=86400) return 'Welcome'
D. from flask import make_response @app.route('/') def index(): resp = make_response('Welcome') resp.set_cookie('visited', 'yes', max_age=86400) return resp

Solution

  1. Step 1: Identify how to create a response object

    Use make_response() to create a response object to set cookies.
  2. Step 2: Set cookie with expiration

    Use set_cookie() with max_age=86400 (seconds in 1 day) to set expiration.
  3. 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 uses make_response and set_cookie with max_age. Others either set cookie on string or request or use wrong syntax.
  4. Final Answer:

    resp.set_cookie('visited', 'yes', max_age=86400) -> Option D
  5. Quick Check:

    Use make_response + set_cookie + max_age = A [OK]
Hint: Use make_response and set_cookie with max_age for expiry [OK]
Common Mistakes:
  • Setting cookie on string instead of response object
  • Trying to set cookie on request object
  • Using resp.cookies dict instead of set_cookie method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard