Q1. What is Flask, and how does it handle HTTP requests?
Flask is a lightweight Python web framework based on WSGI (Web Server Gateway Interface).
It follows a request–response cycle where a client sends an HTTP request, Flask routes it to a view function, processes logic, and returns a response.
Flask uses Werkzeug for request handling and Jinja2 for templating. The framework gives developers full control over components instead of enforcing a rigid structure.
This makes Flask ideal for APIs, microservices, and scalable backend systems.
Q2. How does Flask routing work internally?
Flask routing maps URLs to Python functions using decorators like @app.route(). Internally, Flask stores URL rules and matches incoming requests based on path and HTTP method.
Variable routes allow dynamic URL segments. Flask also supports method-specific routing such as GET, POST, PUT, and DELETE. This routing flexibility is essential for building RESTful APIs.
Q3. Explain the REST API request–response lifecycle in Flask.
In a Flask REST API, the client sends an HTTP request with headers, parameters, and body. Flask parses the request using the request object.
Business logic is executed, often involving databases or services. The response is returned as JSON with a status code. This lifecycle ensures stateless communication between client and server.
Q4. How does Flask integrate with databases in REST APIs?
Flask connects to databases using libraries like SQLAlchemy or direct connectors. The ORM maps Python objects to database tables.
CRUD operations are handled inside route functions or service layers. Flask manages database sessions per request. This integration allows clean and scalable API development.
Q5. Compare Flask and Django for API development.
| Feature | Flask | Django |
| Type | Microframework | Full-stack framework |
| Flexibility | High | Structured |
| Learning Curve | Easy | Moderate |
| Best For | APIs & microservices | Large web apps |
Flask offers flexibility and simplicity, while Django provides built-in components for large applications.
Q6. Compare REST API HTTP methods.
| Method | Purpose | Example Use |
| GET | Fetch data | Retrieve user |
| POST | Create data | Add new user |
| PUT | Update data | Update profile |
| DELETE | Remove data | Delete record |
Using correct HTTP methods ensures REST compliance and clarity.
Q7. Compare request.args, request.form, and request.json.
| Attribute | Data Source | Use Case |
| request.args | URL params | Filters |
| request.form | Form data | HTML forms |
| request.json | JSON body | APIs |
Choosing the correct request object is crucial for API correctness.
Q8. Compare Authentication and Authorization in Flask APIs.
| Aspect | Authentication | Authorization |
| Purpose | Who are you? | What can you do? |
| Example | JWT login | Role access |
| Applied | Before access | After login |
Both are essential for secure APIs.
Q9. What is REST, and why is it used with Flask?
REST (Representational State Transfer) is an architectural style for building scalable APIs. It uses stateless communication and standard HTTP methods.
Flask is ideal for REST APIs due to its lightweight nature. REST improves interoperability between systems. It simplifies frontend-backend communication.
Q10. How does Flask handle JSON responses?
Flask returns JSON using jsonify(), which sets correct headers and converts dictionaries to JSON.
It ensures proper encoding and security. JSON responses are standard in REST APIs. Flask also supports custom status codes. This enables consistent API communication.
Q11. What is Blueprints in Flask, and why are they important?
Blueprints help organize large Flask applications into modules. They separate routes by feature or service.
This improves maintainability and scalability. Blueprints support reusable API components. Large production APIs rely heavily on blueprints.
Q12. What is Flask-RESTful, and when should you use it?
Flask-RESTful simplifies API development by using class-based resources. It provides automatic routing and request parsing.
It enforces REST structure. Flask-RESTful is useful for clean API design. It reduces repetitive code.
Q13. How does Flask manage application configuration?
Flask uses configuration objects and environment variables. Settings like database URI and secret keys are stored securely.
Multiple configurations can be used for dev, test, and production. This ensures flexibility across environments. Configuration management is vital for deployment.
Q14. What is middleware in Flask?
Middleware runs before or after requests. It is used for logging, authentication, and request validation. Flask implements middleware using decorators and WSGI hooks. Middleware helps apply cross-cutting concerns globally. It improves code reuse.
Q15. How do you handle errors in Flask REST APIs?
Flask provides error handlers using @app.errorhandler. APIs return structured JSON error responses. Custom exceptions improve debugging. Proper error handling improves client experience. Status codes should reflect error types.
Q16. What is JWT authentication in Flask APIs?
JWT uses tokens for stateless authentication. The server issues a token after login. Clients include the token in headers for requests. Flask verifies tokens on each request. JWT is scalable and widely used in APIs.
Q17. How do you validate input data in Flask APIs?
Input validation prevents invalid or malicious data. Libraries like Marshmallow or Flask-WTForms are used. Validation occurs before processing logic. Proper validation improves API security. It reduces runtime errors.
Q18. How does Flask support CORS in APIs?
CORS allows cross-origin requests from frontend apps. Flask uses Flask-CORS extension. It sets appropriate headers. CORS is essential for frontend-backend separation. Without it, browsers block requests.
Q19. What is pagination in REST APIs, and why is it needed?
Pagination limits response size for large datasets. It improves performance and reduces bandwidth. Flask handles pagination using query parameters. APIs often return metadata with paginated results. Pagination is critical for scalable APIs.
Q20. How do you deploy a Flask REST API?
Flask APIs are deployed using WSGI servers like Gunicorn or uWSGI. They run behind Nginx for load balancing. Docker is commonly used for containerization. Environment variables manage secrets. Deployment completes the MLOps pipeline.





