使用fastapi,我不知道如何发送多个文件作为响应。例如,要发送单个文件,我将使用类似这样的东西
from fastapi import FastAPI, Response
app = FastAPI()
@app.get("/image_from_id/")
async def image_from_id(image_id: int):
# Get image from the database
img = ...
return Response(content=img, media_type="application/png")
但是,我不确定发送图像列表是什么样的。理想情况下,我想做这样的事情:
@app.get("/images_from_ids/")
async def image_from_id(image_ids: List[int]):
# Get a list of images from the database
images = ...
return Response(content=images, media_type="multipart/form-data")
但是,这会返回错误
def render(self, content: typing.Any) -> bytes:
if content is None:
return b""
if isinstance(content, bytes):
return content
> return content.encode(self.charset)
E AttributeError: 'list' object has no attribute 'encode'