Skip to content
masterclass Buddy

FastAPI · Class

TrustedHostMiddleware

TrustedHostMiddleware checks request Host headers against allowed host names. It helps protect against HTTP Host Header attacks and returns 400 for invalid hosts.

Last updated

Syntax

TrustedHostMiddleware(app, allowed_hosts=None, www_redirect=True)

In-depth API docs are only for subscribers: parameters, examples, common mistakes and interview practice.

Subscribe
  • FastAPI
  • HTTPSRedirectMiddleware
  • GZipMiddleware
  • CORSMiddleware
  • Starlette

Source: Middleware - FastAPI. Checked 2026-11-03.