Please, help me to coupe the problem of blocked code. I am using FastAPI.
I've done with a middleware to log incoming requests and server responses for them.
@app.middleware("http")
async def log_request(request: Request, call_next):
# Code to log incoming request
response = await call_next(request)
# Code to log response
return response
I have an endpoint without dependency injection:
@app.post("/without-dependency-injection", tags=["Test"])
async def check_response_without_using_dependency():
# ... Any code
return {"token":"token123"}
Here is a console with a log written by middleware:
[17.08.2021 13:08:25.08S]: {"event_type": "Incoming request", "id": "3fb33dc0-cb86-49e9-9a0f-c48596e58061"}
[17.08.2021 13:08:25.08S]: {"event_type": "Outgoing response", "id": "3fb33dc0-cb86-49e9-9a0f-c48596e58061"}
INFO: 127.0.0.1:50285 - "POST /without-dependency-injection HTTP/1.1" 200 OK
Everything works well! Middleware catches the request, writes log with a request details, execute request, write log with the server response and return response to a client.
Behavior will change when i add dependency.
This is an endpoint with dependency injection:
@app.post("/generate-token/", tags=["Test"])
async def create_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
# ...
# Code to generate token
return {"token":f"{token}"}
And here is a console log:
[17.08.2021 13:13:08.13S]: {"event_type": "Incoming request", "id": "3b6398de-5b20-40ad-820e-24733425e6c7"}
As you see, there is no "Outgoing response" event. Middleware caught the request, logged incoming request data and sent request to an execution. And a code was blocked... until server get a new request. Only after that client will get a response from a server.
If I remove a middleware or dependency injection, everything works well.
Who knows why the code is blocked until server will get a new request from a client? How to fix this behavior?