custom APIRoute is not work as expected #9454
Answered
by
mateoradman
zffocussss
asked this question in
Questions
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
from fastapi.routing import APIRoute
from fastapi import Body, FastAPI, Request, Response
from typing import Callable, List
class GzipRoute(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
print("custom_route_handler called")
return await original_route_handler(request)
return custom_route_handler
app = FastAPI()
app.router.router_class = GzipRoute
@app.get("/")
async def root():
return {"message": "Hello World"}Descriptionas this link describe, user can get request body and do additional tasks before request go to the bussiness router functions.but it did not work in my case.I suppose it should call the function custom_route_handler, Operating SystemmacOS Operating System Detailsmacos M2 FastAPI Version0.86.0 Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
mateoradman
Apr 27, 2023
Replies: 2 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Hi @zffocussss, you have a typo in your code. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Kludex
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Hi @zffocussss,
you have a typo in your code.
app.router.router_class = ...should beapp.router.route_class = ...