A response object in Flask is what your web app sends back to the user's browser. It tells the browser what to show or do next.
Response object creation in Flask
from flask import Response response = Response(response=None, status=None, headers=None, mimetype=None, content_type=None, direct_passthrough=False)
The response parameter is the content you want to send back, like text or HTML.
You can set status to a number like 200 (OK) or 404 (Not Found).
from flask import Response resp = Response("Hello, world!", status=200, mimetype='text/plain')
from flask import Response resp = Response("<h1>Welcome</h1>", mimetype='text/html')
from flask import Response headers = {'X-Custom-Header': 'MyValue'} resp = Response("Data", headers=headers)
from flask import Response resp = Response(status=404)
This Flask app sends a simple HTML heading as a response when you visit the home page.
from flask import Flask, Response app = Flask(__name__) @app.route('/') def home(): content = "<h1>Hello from Flask!</h1>" return Response(content, status=200, mimetype='text/html') if __name__ == '__main__': app.run(debug=True)
Use mimetype or content_type to tell the browser what kind of content you are sending.
Always set the correct status code to help browsers and tools understand the response.
Flask also provides shortcut functions like flask.jsonify and flask.redirect for common response types.
A Response object is what Flask sends back to the user's browser.
You can customize the content, status code, headers, and content type.
Creating a Response object lets you control exactly what the user receives.
Practice
Response object in Flask?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 BQuick Check:
Response object = send data back [OK]
- Confusing response with request handling
- Thinking Response stores session data
- Assuming Response connects to databases
Solution
Step 1: Recall Response constructor parameters
The Flask Response object accepts astatusparameter to set the HTTP status code.Step 2: Match correct parameter name
Onlystatus=200is valid;code,status_code, andstatare invalid.Final Answer:
return Response('Hello', status=200) -> Option AQuick Check:
Use status= for HTTP code [OK]
- Using incorrect parameter names like code or stat
- Confusing status with status_code
- Omitting the status parameter
from flask import Response
resp = Response('Not Found', status=404)
print(resp.status_code)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
Printingresp.status_codewill output the status code set, which is 404.Final Answer:
404 -> Option AQuick Check:
Status code set = printed status_code [OK]
- Assuming default 200 status code
- Confusing status with response content
- Expecting None or error output
from flask import Response
resp = Response('OK', status_code=200)
return respSolution
Step 1: Check Response constructor parameters
The Response object usesstatusto set HTTP status, notstatus_code.Step 2: Identify the error
Usingstatus_code=200causes a TypeError because it is not a valid parameter.Final Answer:
Response constructor does not accept status_code parameter -> Option CQuick Check:
Use status= not status_code in Response [OK]
- Using status_code instead of status
- Thinking Response content must be bytes
- Assuming Response can't be returned directly
{"success": true} with status 201 and a custom header X-Custom: Yes. Which code snippet correctly does this?Solution
Step 1: Check status parameter correctness
Usestatus=201to set the HTTP status code correctly.Step 2: Verify mimetype and headers usage
mimetype='application/json'correctly sets content type;headersmust 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'}) usescontent_typewhich 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 invalidstatus_code; return Response(json.dumps({"success": true}), status=201, mimetype='application/json', header={'X-Custom': 'Yes'}) uses incorrectheaderkey (should beheaders).Final Answer:
return Response('{"success": true}', status=201, mimetype='application/json', headers={'X-Custom': 'Yes'}) -> Option DQuick Check:
Status=201, mimetype, headers dict correct [OK]
- Using status_code instead of status
- Wrong headers key name (header vs headers)
- Forgetting to set mimetype for JSON
Ready to prove it?
Pass the test and this pattern moves unknown
