不需要声明,响应头中的`Content-Type`就会告诉客户端你的响应是什么类型。 * * * docs 里的 media_type 是通过 response_class 实现的,需要自定义 response_class 才能修改。 from fastapi import FastAPI from fastapi.responses import StreamingResponse app = FastAPI() class MyCustomResponse(StreamingResponse): media_type = "image/jpeg" # 将文件类型写在这里 @app.get("/img", response_class=MyCustomResponse) # 指定 MyCustomResponse def image(): def iterfile(): with open("./image.jpg", mode="rb") as file_like: yield from file_like return MyCustomResponse(iterfile()) 