How does Flask handle query parameters compared to FastAPI?
In Flask, query parameters are read manually from the request object via request.args.get("who"), while FastAPI binds them as direct function parameters like def greet(who: str), letting FastAPI extract and pass the query value automatically.
The examples in the book show that FastAPI and Flask differ mainly in where the query parameter comes from. For the URL /hi?who=World, FastAPI declares the function def greet(who: str) and FastAPI fills who from the query string. Flask requires importing request and then retrieving the value inside the function with who: str = request.args.get("who"). Flask does not use the type annotation for query parsing; request.args is a dict containing the query parameters. Both frameworks end up returning JSON, but Flask needs the explicit request.args access while FastAPI does it through the function signature.
Key points
- Flask uses request.args.get("who") to get a query parameter.
- FastAPI accepts query parameters as direct function arguments, such as def greet(who: str).
- Flask requires importing the request object; FastAPI does not for this case.
- The type hint in Flask is only an annotation, whereas FastAPI uses the signature to bind the query value.
Related questions
FastAPI: Modern Python Web Development
Bill Lubanovic;
First Edition · O'Reilly Media, Inc.