Customizing APIRoute and Request
Middleware can process incoming Request
data and outgoing Response
objects of all API methods in a FastAPI application, except that it cannot manipulate the message body, attach state objects from the Request
data, or modify the response object before the client consumes it. Only APIRoute
and Request
customization can give us a full grasp of how to control the request and response transaction. The control might include determining whether the incoming data is a byte body, form, or JSON and providing an effective logging mechanism, exception handling, content transformation, and extraction.
Managing body, form, or JSON data
Unlike in middleware, customizing APIRoute
does not apply to all the API endpoints. Implementing APIRoute
for some APIRouter
will only impose new routing rules to those affected endpoints, while the other services can pursue the default request and response process. For instance, the following customization is responsible...