0
0
Flaskframework~20 mins

Setting and reading cookies in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cookie Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask route when setting a cookie?

Consider this Flask route that sets a cookie named user with value alice. What will the response headers include?

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

@app.route('/')
def index():
    resp = make_response('Hello World')
    resp.set_cookie('user', 'alice')
    return resp
AThe response will include a Set-Cookie header with 'user=alice'
BThe response will include a Set-Cookie header with 'user=alice; Max-Age=3600'
CThe response will not include any Set-Cookie header
DThe response will include a Set-Cookie header with 'user=alice; HttpOnly; Secure'
Attempts:
2 left
💡 Hint

Check how set_cookie works by default without extra parameters.

state_output
intermediate
2:00remaining
What value does the Flask route read from the cookie?

Given this Flask route that reads a cookie named session_id, what will be returned if the cookie is not set?

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

@app.route('/read')
def read_cookie():
    session = request.cookies.get('session_id', 'none')
    return f'Session: {session}'
AKeyError exception
BSession:
CSession: None
DSession: none
Attempts:
2 left
💡 Hint

Look at the default value provided to get method.

📝 Syntax
advanced
2:00remaining
Which option correctly sets a cookie with a 1-hour expiration?

Choose the correct Flask code snippet that sets a cookie named token with value abc123 that expires in 3600 seconds.

Aresp.set_cookie('token', 'abc123', maxage=3600)
Bresp.set_cookie('token', 'abc123', expires=3600)
Cresp.set_cookie('token', 'abc123', max_age=3600)
Dresp.set_cookie('token', 'abc123', expire=3600)
Attempts:
2 left
💡 Hint

Check the exact parameter name for cookie expiration in Flask's set_cookie.

🔧 Debug
advanced
2:00remaining
Why does this Flask code fail to read the cookie?

Given this Flask route, why does it always print Cookie not found even if the user cookie is set?

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

@app.route('/check')
def check_cookie():
    if 'User' in request.cookies:
        return f"User: {request.cookies['User']}"
    else:
        return 'Cookie not found'
ACookies must be accessed with get() method, not by key
BCookie keys are case-sensitive; 'User' is not the same as 'user'
CFlask does not support reading cookies from request
DThe cookie is not sent because the route is missing @app.before_request
Attempts:
2 left
💡 Hint

Check the case of the cookie key used in the condition and in the return statement.

🧠 Conceptual
expert
2:00remaining
What happens if you set a cookie with the Secure flag but access the site over HTTP?

In Flask, if you set a cookie with secure=True, what will happen when the client accesses the site over plain HTTP?

AThe cookie will not be sent by the browser on HTTP requests
BThe cookie will be sent normally on HTTP and HTTPS
CThe cookie will be deleted immediately by the browser
DThe server will reject the request due to missing secure cookie
Attempts:
2 left
💡 Hint

Think about what the Secure flag means for cookies in browsers.