Performance: Response object creation
This affects server response time and how quickly the browser receives and renders content.
def view(): return 'Hello World', 200, {'Content-Type': 'text/plain'}
from flask import Response def view(): data = 'Hello World' resp = Response(data) resp.headers['Content-Type'] = 'text/plain' return resp
| Pattern | Server Processing | Response Size | Network Delay | Verdict |
|---|---|---|---|---|
| Manual Response object creation | Higher (extra object creation) | Same | Same | [!] OK |
| Return tuple with data and headers | Lower (direct return) | Same | Same | [OK] Good |
Response object in Flask?status parameter to set the HTTP status code.status=200 is valid; code, status_code, and stat are invalid.from flask import Response
resp = Response('Not Found', status=404)
print(resp.status_code)resp.status_code will output the status code set, which is 404.from flask import Response
resp = Response('OK', status_code=200)
return respstatus to set HTTP status, not status_code.status_code=200 causes a TypeError because it is not a valid parameter.{"success": true} with status 201 and a custom header X-Custom: Yes. Which code snippet correctly does this?status=201 to set the HTTP status code correctly.mimetype='application/json' correctly sets content type; headers must be a dictionary with correct key.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).Pass the test and this pattern moves unknown