0
0
FastAPIframework~10 mins

Trusted host middleware in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the TrustedHostMiddleware from FastAPI.

FastAPI
from fastapi.middleware.[1] import TrustedHostMiddleware
Drag options to blanks, or click blank then click option'
Atrusted_host
Btrustedhostmiddleware
Ctrustedhost
Dtrustedhost_middleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using camel case or concatenated words in import path.
2fill in blank
medium

Complete the code to add TrustedHostMiddleware to the FastAPI app with allowed hosts.

FastAPI
app.add_middleware(TrustedHostMiddleware, allowed_hosts=[1])
Drag options to blanks, or click blank then click option'
A['example.com']
B['example.com', 'localhost']
C['*']
D['example.com', 'localhost', '*']
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' which disables host checking.
3fill in blank
hard

Fix the error in the middleware setup by completing the code correctly.

FastAPI
app = FastAPI()
app.add_middleware([1], allowed_hosts=['myapp.com'])
Drag options to blanks, or click blank then click option'
Atrusted_host_middleware
BtrustedHostMiddleware
CTrustedHost
DTrustedHostMiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect casing for middleware class.
4fill in blank
hard

Fill both blanks to create a FastAPI app and add TrustedHostMiddleware with allowed hosts.

FastAPI
app = [1]()
app.add_middleware([2], allowed_hosts=['api.example.com'])
Drag options to blanks, or click blank then click option'
AFastAPI
Btrusted_host_middleware
CTrustedHostMiddleware
DFastApi
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong casing for FastAPI or middleware class.
5fill in blank
hard

Fill all three blanks to define a FastAPI app, add TrustedHostMiddleware, and specify allowed hosts.

FastAPI
from fastapi import [1]
from fastapi.middleware.trusted_host import [2]

app = [3]()
app.add_middleware(TrustedHostMiddleware, allowed_hosts=['localhost', 'mydomain.com'])
Drag options to blanks, or click blank then click option'
AFastAPI
BTrustedHostMiddleware
CFastApi
DtrustedHostMiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect casing in imports or app creation.