5. You want a FastAPI route to return JSON by default but HTML if a query parameter
?format=html is given. Which approach correctly implements this with multiple response types?
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, HTMLResponse
app = FastAPI()
@app.get('/info', responses={200: {'content': {'application/json': {}, 'text/html': {}}}})
async def info(request: Request):
format = request.query_params.get('format')
if format == 'html':
# What to return here?
else:
return JSONResponse(content={'info': 'data'})