Challenge - 5 Problems
Flask Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask response code?
Consider this Flask route code. What will the client receive as the response body?
Flask
from flask import Flask, Response app = Flask(__name__) @app.route('/') def home(): return Response('Hello World', status=200, mimetype='text/plain')
Attempts:
2 left
💡 Hint
Check the mimetype and status parameters in the Response constructor.
✗ Incorrect
The Response object is created with the string 'Hello World', status 200, and mimetype 'text/plain'. This means the client receives plain text with that exact body and status code.
📝 Syntax
intermediate2:00remaining
Which option correctly creates a JSON response in Flask?
You want to return a JSON response with data {'success': True} and status 201. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember that Response expects a string or bytes for the body, not a dict.
✗ Incorrect
Option A correctly converts the dictionary to a JSON string using json.dumps and sets the mimetype to 'application/json'. Option A passes a dict directly causing a TypeError. Option A misses mimetype and passes dict. Option A passes JSON string but misses mimetype, so content type is default.
🔧 Debug
advanced2:00remaining
Why does this Flask response raise a TypeError?
Examine the code below. Why does it raise a TypeError when the route is accessed?
Flask
from flask import Flask, Response app = Flask(__name__) @app.route('/data') def data(): return Response({'key': 'value'}, status=200, mimetype='application/json')
Attempts:
2 left
💡 Hint
Check the type of the first argument passed to Response.
✗ Incorrect
Response expects the body to be a string or bytes. Passing a dict directly causes a TypeError because Flask cannot convert it automatically.
❓ state_output
advanced2:00remaining
What is the status code of this Flask response?
Given this Flask route, what status code will the client receive?
Flask
from flask import Flask, Response app = Flask(__name__) @app.route('/custom') def custom(): resp = Response('Done') resp.status_code = 202 return resp
Attempts:
2 left
💡 Hint
Look at how the status_code attribute is set before returning the response.
✗ Incorrect
The Response object is created with default status 200, but then status_code is changed to 202 before returning. So the client receives status 202.
🧠 Conceptual
expert2:00remaining
Which statement about Flask Response objects is true?
Select the one true statement about creating Response objects in Flask.
Attempts:
2 left
💡 Hint
Think about what types Response accepts for its body parameter.
✗ Incorrect
Flask Response expects the body to be a string or bytes. Passing other types like dict causes errors. jsonify() is a helper but not mandatory. Status code must be set before returning the Response object.