Overview
Flask providesRequest and Response objects that wrap Werkzeug’s WSGI request and response handling with Flask-specific enhancements.
Request Object
Therequest object is a global proxy that provides access to incoming request data.
Accessing Request Data
Request Properties
Fromwrappers.py:18-54, the Flask Request class provides:
Request Data Sources
Query String
Access GET parameters:Form Data
Access POST form data:JSON Data
Parse JSON request bodies:File Uploads
Handle uploaded files:Raw Data
Access raw request body:Request Limits
Fromwrappers.py:60-144, Flask provides security limits:
Blueprint Context
Fromwrappers.py:162-195, access blueprint information:
Response Object
TheResponse object represents the HTTP response sent to the client.
Creating Responses
Simple Responses
Response Tuples
Return status code and headers:Response Properties
Fromwrappers.py:222-258:
Setting Cookies
Manage cookies in responses:make_response()
Convert return values to Response objects (fromapp.py:1224-1273):
JSON Responses
Flask provides convenient JSON handling:Streaming Responses
Stream large responses:File Downloads
Send files to clients:Request Context
Therequest object is context-local, available only during request handling:
Modifying Responses
After Request Handlers
Modify all responses:Per-Request Modifications
Modify specific request responses:Best Practices
Validate Input
Always validate and sanitize request data
Set Limits
Configure MAX_CONTENT_LENGTH to prevent DOS attacks
Use get() Method
Use
.get() instead of dict access to avoid KeyErrorSecure Cookies
Always use secure, httponly cookies for sensitive data
Related Concepts
- Routing - URL routing to view functions
- Sessions - Managing user sessions
- Request Context - Understanding request context
