0
0
FastAPIframework~10 mins

Including routers in main app 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 FastAPI and create an app instance.

FastAPI
from fastapi import [1]

app = [1]()
Drag options to blanks, or click blank then click option'
ARequest
BFastAPI
CAPIRouter
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing APIRouter instead of FastAPI
Forgetting to call FastAPI() as a function
2fill in blank
medium

Complete the code to import APIRouter and create a router instance.

FastAPI
from fastapi import [1]

router = [1]()
Drag options to blanks, or click blank then click option'
AAPIRouter
BRequest
CDepends
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Importing FastAPI instead of APIRouter
Not calling APIRouter() as a function
3fill in blank
hard

Fix the error in including the router in the main app.

FastAPI
app = FastAPI()

app.[1](router)
Drag options to blanks, or click blank then click option'
Aregister_router
Badd_router
Cinclude_router
Dattach_router
Attempts:
3 left
💡 Hint
Common Mistakes
Using add_router instead of include_router
Using register_router or attach_router which do not exist
4fill in blank
hard

Fill both blanks to define a route in the router and include it in the app.

FastAPI
from fastapi import FastAPI, APIRouter

router = APIRouter()

@router.[1]("/")
async def root():
    return {"message": "Hello"}

app = FastAPI()
app.[2](router)
Drag options to blanks, or click blank then click option'
Aget
Binclude_router
Cpost
Dadd_router
Attempts:
3 left
💡 Hint
Common Mistakes
Using @router.post instead of @router.get for a GET route
Using add_router instead of include_router
5fill in blank
hard

Fill all three blanks to create a router with a prefix and include it in the app.

FastAPI
from fastapi import FastAPI, APIRouter

router = APIRouter(prefix=[1])

@router.get("/")
async def read_items():
    return {"items": []}

app = FastAPI()
app.[2](router, [3]="/items")
Drag options to blanks, or click blank then click option'
A"/api"
Binclude_router
Cprefix
D"/items"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around prefix strings
Using add_router instead of include_router
Confusing prefix argument placement