A Response object in Flask represents the HTTP response sent back to the client. It contains data like the content, status code, and headers.
Click to reveal answer
beginner
How do you create a simple Response object with custom text in Flask?
You can create it by using <code>from flask import Response</code> and then calling Response('Your text here'). For example: <code>return Response('Hello, world!')</code>
Click to reveal answer
intermediate
Which parameters can you set when creating a Response object in Flask?
You can set the response body (data), status code (like 200, 404), and headers (like content-type). For example: Response('data', status=404, headers={'Content-Type': 'text/plain'})
Click to reveal answer
intermediate
What is the difference between returning a string and returning a Response object in Flask?
Returning a string lets Flask create a default Response object for you. Returning a Response object gives you full control over status codes and headers.
Click to reveal answer
beginner
How can you set a custom HTTP status code using a Response object in Flask?
Pass the status code as the status parameter when creating the Response. Example: Response('Not found', status=404)
Click to reveal answer
Which import is needed to create a Response object in Flask?
Afrom flask import Response
Bimport Response from flask
Cfrom flask.response import ResponseObject
Dimport flask.Response
✗ Incorrect
The correct import is from flask import Response.
What is the default HTTP status code if you return a string from a Flask view?
A404
B500
C200
D302
✗ Incorrect
Flask uses status code 200 (OK) by default when returning a string.
How do you add a custom header to a Response object in Flask?
ASet headers after returning the response
BUse the headers parameter with a dictionary when creating Response
CHeaders cannot be customized in Flask
DUse a special Flask header function
✗ Incorrect
You add headers by passing a dictionary to the headers parameter in Response.
Which of these is a valid way to create a Response with status 201?
AResponse('Created', status=201)
BResponse('Created', code=201)
CResponse('Created', http_status=201)
DResponse('Created', status_code=201)
✗ Incorrect
The correct parameter name is status.
If you want to return JSON data with a Response object, what should you set in headers?
A{'Content-Type': 'application/xml'}
B{'Content-Type': 'text/html'}
C{'Content-Type': 'text/plain'}
D{'Content-Type': 'application/json'}
✗ Incorrect
JSON responses should have the header Content-Type: application/json.
Explain how to create a Response object in Flask with custom text, status code, and headers.
Think about the parameters Response accepts.
You got /4 concepts.
Describe the difference between returning a string and returning a Response object in a Flask view function.
Consider control over HTTP details.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Response object in Flask?
easy
A. To store user session information
B. To send data back to the user's browser
C. To receive data from the user's browser
D. To connect to a database
Solution
Step 1: Understand Flask's response role
A Response object in Flask is what the server sends back to the user's browser after processing a request.
Step 2: Identify the correct purpose
Since the Response object controls what the user receives, it is used to send data back to the browser.
Final Answer:
To send data back to the user's browser -> Option B
Quick Check:
Response object = send data back [OK]
Hint: Response objects send data back to the browser [OK]
Common Mistakes:
Confusing response with request handling
Thinking Response stores session data
Assuming Response connects to databases
2. Which of the following is the correct way to create a Response object with the text 'Hello' and status code 200 in Flask?
easy
A. return Response('Hello', status=200)
B. return Response('Hello', code=200)
C. return Response('Hello', status_code=200)
D. return Response('Hello', stat=200)
Solution
Step 1: Recall Response constructor parameters
The Flask Response object accepts a status parameter to set the HTTP status code.
Step 2: Match correct parameter name
Only status=200 is valid; code, status_code, and stat are invalid.
Final Answer:
return Response('Hello', status=200) -> Option A
Quick Check:
Use status= for HTTP code [OK]
Hint: Use status= to set HTTP status code in Response [OK]
Common Mistakes:
Using incorrect parameter names like code or stat
Confusing status with status_code
Omitting the status parameter
3. What will be the output status code when this Flask code runs?
from flask import Response
resp = Response('Not Found', status=404)
print(resp.status_code)
medium
A. 404
B. 200
C. 500
D. None
Solution
Step 1: Check Response creation
The Response is created with the text 'Not Found' and status=404, which sets the HTTP status code to 404.
Step 2: Access status_code attribute
Printing resp.status_code will output the status code set, which is 404.
Final Answer:
404 -> Option A
Quick Check:
Status code set = printed status_code [OK]
Hint: Response.status_code shows the HTTP status code [OK]
Common Mistakes:
Assuming default 200 status code
Confusing status with response content
Expecting None or error output
4. Identify the error in this Flask code snippet:
from flask import Response
resp = Response('OK', status_code=200)
return resp
medium
A. Response object cannot be returned directly
B. Response content must be bytes, not string
C. Response constructor does not accept status_code parameter
D. Missing import for Flask app
Solution
Step 1: Check Response constructor parameters
The Response object uses status to set HTTP status, not status_code.
Step 2: Identify the error
Using status_code=200 causes a TypeError because it is not a valid parameter.
Final Answer:
Response constructor does not accept status_code parameter -> Option C
Quick Check:
Use status= not status_code in Response [OK]
Hint: Use status=, not status_code= in Response constructor [OK]
Common Mistakes:
Using status_code instead of status
Thinking Response content must be bytes
Assuming Response can't be returned directly
5. You want to create a Flask Response object that returns JSON data {"success": true} with status 201 and a custom header X-Custom: Yes. Which code snippet correctly does this?
hard
A. return Response(json.dumps({"success": true}), status=201, content_type='application/json', headers={'X-Custom': 'Yes'})
B. return Response(json.dumps({"success": true}), status=201, mimetype='application/json', header={'X-Custom': 'Yes'})
C. return Response('{"success": true}', status_code=201, mimetype='application/json', headers={'X-Custom': 'Yes'})
D. return Response('{"success": true}', status=201, mimetype='application/json', headers={'X-Custom': 'Yes'})
Solution
Step 1: Check status parameter correctness
Use status=201 to set the HTTP status code correctly.
Step 2: Verify mimetype and headers usage
mimetype='application/json' correctly sets content type; headers must be a dictionary with correct key.
Step 3: Identify errors in other options
return Response(json.dumps({"success": true}), status=201, content_type='application/json', headers={'X-Custom': 'Yes'}) uses content_type which is valid but misses importing json and json.dumps usage is not shown; return Response('{"success": true}', status_code=201, mimetype='application/json', headers={'X-Custom': 'Yes'}) uses invalid status_code; return Response(json.dumps({"success": true}), status=201, mimetype='application/json', header={'X-Custom': 'Yes'}) uses incorrect header key (should be headers).
Final Answer:
return Response('{"success": true}', status=201, mimetype='application/json', headers={'X-Custom': 'Yes'}) -> Option D
Quick Check:
Status=201, mimetype, headers dict correct [OK]
Hint: Use status=, mimetype=, and headers={} for custom Response [OK]
Common Mistakes:
Using status_code instead of status
Wrong headers key name (header vs headers)
Forgetting to set mimetype for JSON
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.