How can I get the name of a route/endpoint using FastAPI/Starlette? I have access to the Request object and I need this information in one of my middlewares. For example, if I hit services/1, I should then be able to get the abc name. Is this possible in FastAPI? Update 1: Output of request.scope Update 2: Providing middleware code where
Tag: starlette
FastAPI does not replace “+” plus symbol in GET request
I understand this is not a FastAPI issue, but how to avoid this using FastAPI? For example: Issuing the following request: returns: Answer The plus sign (+) has a semantic meaning in the query string, i.e., representing the space character. Similarly, the ampersand sign (&), which is used to separate the various key=value pairs in the query string. When a
How to pass URL as a path parameter to a FastAPI route?
I have created a simple API using FastAPI, and I am trying to pass a URL to a FastAPI route as an arbitrary path parameter. When I test it, it doesn’t work and throws an error. I am testing it this way: Answer Option 1 You can simply use the path convertor to capture arbitrary paths. As per Starlette documentation,
FastAPI – How to get app instance inside a router?
I want to get the app instance in my router file, what should I do ? My main.py is as follows: Now I want to use app.machine_learning_model in some_router’s file , what should I do ? Answer You should store the model on the app instance using the generic app.state attribute, as described in the documentation (see State class implementation
Send query params from Jinja template
I can’t figure out if it’s possible to pass query parameters from a static html page to a view for processing. I have now implemented the functionality I need using path parameters. I want to do the same, but with query parameters main.py events.py routes.py html It’s works with “POST /event/invite/15/sender/3 HTTP/1.1” as a sample Try with query params And
Get starlette request body in the middleware context
I have such middleware So the line body = await request.body() freezes all requests that have body and I have 504 from all of them. How can I safely read the request body in this context? I just want to log request parameters. Answer I would not create a Middleware that inherits from BaseHTTPMiddleware since it has some issues, FastAPI