小编nic*_*hen的帖子

FastAPI - 如何在中间件中获取响应正文

有没有办法在中间件中获取响应内容?以下代码是从此处复制的。

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()

    response = await call_next(request)

    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response
Run Code Online (Sandbox Code Playgroud)

python middleware response fastapi

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

fastapi ×1

middleware ×1

python ×1

response ×1