Bird
0
0

In this FastAPI app, what will happen if a request is made from host 'unauthorized.com'?

medium📝 component behavior Q4 of 15
FastAPI - Middleware and Hooks
In this FastAPI app, what will happen if a request is made from host 'unauthorized.com'? ```python from fastapi import FastAPI from starlette.middleware.trustedhost import TrustedHostMiddleware app = FastAPI() app.add_middleware(TrustedHostMiddleware, allowed_hosts=['myapp.com', 'localhost']) ```
AThe request will be redirected to 'myapp.com'
BThe request will be allowed and processed normally
CThe request will be blocked with a 400 Bad Request error
DThe request will cause the server to crash
Step-by-Step Solution
Solution:
  1. Step 1: Check allowed hosts

    Only 'myapp.com' and 'localhost' are allowed.
  2. Step 2: Behavior on disallowed host

    Requests from hosts not in the list are rejected with 400 error.
  3. Final Answer:

    The request will be blocked with a 400 Bad Request error -> Option C
  4. Quick Check:

    Disallowed hosts get 400 error [OK]
Quick Trick: Requests from unlisted hosts return 400 error [OK]
Common Mistakes:
MISTAKES
  • Assuming disallowed hosts are redirected
  • Thinking requests are allowed by default
  • Believing server crashes on bad hosts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes