tezvyn:

FastAPI: Returning HTML with HTMLResponse

Source: fastapi.tiangolo.combeginner

Override FastAPI's default JSON output by using `HTMLResponse` to return a raw HTML string directly from an endpoint. It's for simple status pages or server-side rendered components.

`HTMLResponse` overrides FastAPI's default JSON behavior, letting an endpoint return a raw HTML string that browsers render as a webpage. It's ideal for simple health check pages, returning server-generated HTML fragments for frameworks like HTMX, or serving basic static documents. The main pitfall is overusing it for complex UIs or including unescaped user data, which creates security risks. For anything beyond simple snippets, use a proper templating engine like Jinja2 to separate logic from presentation.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

FastAPI: Returning HTML with HTMLResponse · Tezvyn