Adding middleware ended up dirtier than if I wrote the code for FastAPI serving myself, because the FastAPIMiddleware seems to be applied after the serializer, and I wanted a middleware applied before serializers (for authentication purposes).
Other question:
If I'm understanding correctly, the middleware on_request methods are called after the request_serializer. Is there a way to add layers before the request_serializer.
I was trying to add authentification middleware (for FastApi). I first tried to inherit from FastAPIMiddleware , and do a test in the on_request method (to test if the field api_key of the request header has the correct value). The problem is, since I am using the pil_numpy serializer, the request argument of on_request is a Numpy array.
Other question: If I'm understanding correctly, the middleware
on_request
methods are called after the request_serializer. Is there a way to add layers before therequest_serializer
.I was trying to add authentification middleware (for FastApi). I first tried to inherit from
FastAPIMiddleware
, and do a test in theon_request
method (to test if the fieldapi_key
of the request header has the correct value). The problem is, since I am using thepil_numpy
serializer, therequest
argument ofon_request
is a Numpy array.So the only solution I have found is:
Is there a better way to do it?
reported in discord