fastapi 的 swagger response 怎么声明 media_type?-灵析社区

Kapp啊$0920

不需要声明,响应头中的`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()) ![截屏2023-11-16 14.04.01.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20241120/3dd3f5326054ee43856069cf328ea2bf.png)

阅读量:1

点赞量:0

问AI