| 1 |
from fastapi import FastAPI |
| 2 |
from fastapi.responses import FileResponse |
| 3 |
from pydantic import BaseModel |
| 4 |
|
| 5 |
|
| 6 |
class Item(BaseModel): |
| 7 |
id: str |
| 8 |
value: str |
| 9 |
|
| 10 |
|
| 11 |
app = FastAPI() |
| 12 |
|
| 13 |
|
| 14 |
@app.get( |
| 15 |
"/items/{item_id}", |
| 16 |
response_model=Item, |
| 17 |
responses={ |
| 18 |
200: { |
| 19 |
"content": {"image/png": {}}, |
| 20 |
"description": "Return the JSON item or an image.", |
| 21 |
} |
| 22 |
}, |
| 23 |
) |
| 24 |
async def read_item(item_id: str, img: bool | None = None): |
| 25 |
if img: |
| 26 |
return FileResponse("image.png", media_type="image/png") |
| 27 |
else: |
| 28 |
return {"id": "foo", "value": "there goes my hero"} |