Bird
0
0

Which of the following is the correct way to add TrustedHostMiddleware to a FastAPI app?

easy📝 Syntax Q12 of 15
FastAPI - Middleware and Hooks
Which of the following is the correct way to add TrustedHostMiddleware to a FastAPI app?
Aapp.middleware(TrustedHostMiddleware, allowed=['example.com'])
Bapp.add_middleware(TrustedHostMiddleware, allowed_hosts=['example.com'])
Capp.use(TrustedHostMiddleware, hosts=['example.com'])
Dapp.add_middleware(TrustedHostMiddleware, hosts=['example.com'])
Step-by-Step Solution
Solution:
  1. Step 1: Recall FastAPI middleware syntax

    FastAPI uses app.add_middleware() with the middleware class and keyword arguments.
  2. Step 2: Check correct argument name

    The correct argument for allowed hosts is allowed_hosts, not hosts or allowed.
  3. Final Answer:

    app.add_middleware(TrustedHostMiddleware, allowed_hosts=['example.com']) -> Option B
  4. Quick Check:

    Use add_middleware with allowed_hosts = C [OK]
Quick Trick: Use add_middleware and allowed_hosts keyword [OK]
Common Mistakes:
MISTAKES
  • Using wrong method like app.use()
  • Passing 'hosts' instead of 'allowed_hosts'
  • Incorrect argument names like 'allowed'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes