0
0
Flaskframework~20 mins

Response object creation in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Response Master
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 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')
AA JSON response with body 'Hello World' and status 200
BA plain text response with body 'Hello World' and status 200
CAn HTML response with body 'Hello World' and status 404
DA plain text response with empty body and status 200
Attempts:
2 left
💡 Hint
Check the mimetype and status parameters in the Response constructor.
📝 Syntax
intermediate
2: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?
Areturn Response(json.dumps({'success': True}), status=201, mimetype='application/json')
Breturn Response(json.dumps({'success': True}), status=201)
Creturn Response({'success': True}, status=201)
Dreturn Response({'success': True}, status=201, mimetype='application/json')
Attempts:
2 left
💡 Hint
Remember that Response expects a string or bytes for the body, not a dict.
🔧 Debug
advanced
2: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')
AThe mimetype 'application/json' is invalid and causes TypeError
BStatus code 200 is not allowed with Response and causes TypeError
CResponse body must be a string or bytes, not a dict, causing TypeError
DFlask requires jsonify() to create any Response, else TypeError occurs
Attempts:
2 left
💡 Hint
Check the type of the first argument passed to Response.
state_output
advanced
2: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
A202
B500
C200
DNone
Attempts:
2 left
💡 Hint
Look at how the status_code attribute is set before returning the response.
🧠 Conceptual
expert
2:00remaining
Which statement about Flask Response objects is true?
Select the one true statement about creating Response objects in Flask.
ASetting the status code after returning the Response object changes the response sent to client.
BResponse objects can be created with any data type as body; Flask converts it automatically.
CYou must always use Flask's jsonify() to create a Response; Response() cannot handle JSON strings.
DResponse objects require the body to be bytes or string; passing other types causes errors.
Attempts:
2 left
💡 Hint
Think about what types Response accepts for its body parameter.