Understanding routing in FastAPI
A route is defined to accept requests from an HTTP request method and optionally take parameters. When a request is sent to a route, the application checks whether the route is defined before processing the request in the route handler. On the other hand, a route handler is a function that processes the request sent to the server. An example of a route handler is a function that retrieves records from a database when a request is sent to a router via a route.
What are HTTP request methods?
HTTP methods are identifiers for indicating the type of action to be carried out. The standard methods include GET
, POST
, PUT
, PATCH
, and DELETE
. You can learn more about HTTP methods at https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods.
Routing example
In the Project scaffolding section in the previous chapter, we built a single route application. The routing was handled by the FastAPI()
instance initiated in the app
variable:
from fastapi...