Resources, Routing & Request Parsing
A Thin Layer on Top of Flask
Flask-RESTful is a Flask extension adding REST-specific conventions (resource classes, request parsing, response marshaling) on top of Flask's core routing, rather than replacing Flask — the same "extension, not replacement" pattern as Flask-SQLAlchemy or Flask-Login.
Resource Classes: HTTP Verbs as Methods
from flask_restful import Resource, Api
api = Api(app)
class UserResource(Resource):
def get(self, user_id):
return {"id": user_id, "name": "Alice"}
def delete(self, user_id):
return "", 204
api.add_resource(UserResource, "/users/<int:user_id>")A Resource class groups all HTTP verbs for one endpoint as methods (get, post, put, delete) rather than separate Flask view functions checking request.method manually — a GET request calls get(), a DELETE calls delete(), and so on. add_resource registers the class against one or more URL rules, including any parameters.
Parsing Request Input
from flask_restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument("email", type=str, required=True)
parser.add_argument("age", type=int)
class UserResource(Resource):
def post(self):
args = parser.parse_args() # 400 with clear error if invalid/missingreqparse declares expected arguments, types, and required flags up front, validating and extracting them in one call — catching malformed or missing input immediately with a clear error, rather than scattering ad hoc checks on request.json throughout the method body.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free