繁体   English   中英

如何将自定义标头添加到 fastapi 中的 static 文件

[英]How to add custom headers to static files in fastapi

我们使用以下方法从目录app.mount("/static", StaticFiles(directory="static"), name="static")挂载 static 文件

在向客户端发送响应之前,我们使用通用方法为安全相关步骤发送了 api 的自定义标头。

有没有办法将安全标头发送到 css/js/html static 文件相同类型的标头。

只想知道将自定义标头发送到 static 文件的方式或方法。

像这样

from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get("/static/{file_path:path}")
async def function(file_path: str):
    response = FileResponse(f"static/{file_path}")
    response.headers["X-Custom-Header"] = "Your custom header value"
    return response

你可以在这里看到更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM