I have a simple incoming post request to my fast api app.
async def post(request: Request):
... do something
I want to call a function and initialize a variable with content from the header when the request comes in and use it then inside the function. An idea was to use the decorator but maybe I am not family enough with python, I don't get it work. What I want is something like that, does anyone know if this is possible?
class SimpleHandler:
def __init__(self, text):
self.text = text
def say_hello():
return self.text
def getHandler(header):
return SimpleHandler(header.header_attribute)
@app.post("/")
async def get_body(request: Request, simple_handler: getHandler(header)):
result = simple_handler.say_hello()
...
return result
The solution was to use a middleware: https://fastapi.tiangolo.com/tutorial/middleware/