Careeroza
Response Types
Beyond JSON
Example
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, PlainTextResponse, RedirectResponse
app = FastAPI()
@app.get("/hello", response_class=PlainTextResponse)
def hello():
return "Hello as plain text"
@app.get("/page", response_class=HTMLResponse)
def page():
return "<h1>Hello HTML</h1>"
@app.get("/old-docs")
def old_docs():
return RedirectResponse("/docs", status_code=301)Requests and responses
GET /hello → 200 Hello as plain text GET /page → 200 <h1>Hello HTML</h1> GET /old-docs → 301
Others: FileResponse (downloads), StreamingResponse (large or generated content), and headers/cookies through the Response object.