Challenge - 5 Problems
Cookie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Flask route do with cookies?
Consider this Flask route that sets a cookie. What will the browser receive after visiting this route?
Flask
from flask import Flask, make_response app = Flask(__name__) @app.route('/set') def set_cookie(): resp = make_response('Cookie is set') resp.set_cookie('user', 'alice') return resp
Attempts:
2 left
💡 Hint
Look at how make_response and set_cookie work together.
✗ Incorrect
The make_response function creates a response object. Using set_cookie adds a Set-Cookie header. The browser receives this header and stores the cookie. The response body is 'Cookie is set'.
❓ state_output
intermediate2:00remaining
What value does the server read from the cookie?
Given this Flask route that reads a cookie, what will be printed if the browser sends a cookie user=bob?
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/get') def get_cookie(): user = request.cookies.get('user') return f'User is {user}'
Attempts:
2 left
💡 Hint
Check how request.cookies.get works when the cookie exists.
✗ Incorrect
request.cookies.get('user') returns the value of the cookie named 'user'. If the browser sends user=bob, the value is 'bob'.
📝 Syntax
advanced2:00remaining
Which option correctly deletes a cookie in Flask?
You want to delete the cookie named 'session' in Flask. Which code snippet correctly does this?
Flask
from flask import Flask, make_response app = Flask(__name__) @app.route('/logout') def logout(): resp = make_response('Logged out') # delete cookie here return resp
Attempts:
2 left
💡 Hint
Look for the Flask Response method that removes cookies.
✗ Incorrect
Flask's Response object has a delete_cookie method to remove cookies by setting expiration in the past.
🔧 Debug
advanced2:00remaining
Why does this cookie not get set in the browser?
This Flask code tries to set a cookie but the browser never stores it. What is the problem?
Flask
from flask import Flask, make_response app = Flask(__name__) @app.route('/set') def set_cookie(): resp = make_response('Setting cookie') resp.set_cookie('token', 'abc123', httponly=True, secure=True) return 'Cookie set'
Attempts:
2 left
💡 Hint
Check what the route returns.
✗ Incorrect
The route returns a plain string, not the response object with the cookie header. The cookie header is lost.
🧠 Conceptual
expert2:00remaining
How does the browser decide which cookies to send with a request?
When a browser makes an HTTP request, which cookies does it include in the request headers?
Attempts:
2 left
💡 Hint
Think about cookie scope and security flags.
✗ Incorrect
Browsers send cookies only if the cookie domain and path match the request URL, and Secure cookies only over HTTPS. HttpOnly affects JavaScript access, not sending.