0
0
FastAPIframework~10 mins

Why advanced patterns solve real problems in FastAPI - Test Your Understanding

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

Complete the code to create a basic FastAPI app instance.

FastAPI
from fastapi import [1]
app = [1]()
Drag options to blanks, or click blank then click option'
ARequest
BDepends
CResponse
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of FastAPI
Using Response to create app
2fill in blank
medium

Complete the code to define a GET endpoint that returns a greeting.

FastAPI
@app.[1]("/")
async def read_root():
    return {"message": "Hello World"}
Drag options to blanks, or click blank then click option'
Aget
Bput
Cpost
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using post decorator for GET endpoint
Misspelling the decorator name
3fill in blank
hard

Fix the error in the dependency injection by completing the code.

FastAPI
from fastapi import Depends

def common_parameters(q: str = None):
    return {"q": q}

@app.get("/items/")
async def read_items(params: dict = [1](common_parameters)):
    return params
Drag options to blanks, or click blank then click option'
AResponse
BRequest
CDepends
DBody
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request or Response instead of Depends
Forgetting to import Depends
4fill in blank
hard

Fill both blanks to create a reusable router and include it in the main app.

FastAPI
from fastapi import APIRouter

router = [1]()

@router.get("/users")
async def get_users():
    return ["Alice", "Bob"]

app.[2](router)
Drag options to blanks, or click blank then click option'
AAPIRouter
BDepends
Cinclude_router
Dadd_route
Attempts:
3 left
💡 Hint
Common Mistakes
Using Depends instead of APIRouter
Using add_route instead of include_router
5fill in blank
hard

Fill all three blanks to create a Pydantic model, use it in a POST endpoint, and return the data.

FastAPI
from pydantic import BaseModel
from fastapi import FastAPI

app = FastAPI()

class Item([1]):
    name: str
    price: float

@app.post("/items/")
async def create_item(item: [2]):
    return {"item_name": item.[3]
Drag options to blanks, or click blank then click option'
ABaseModel
BItem
Cname
DFastAPI
Attempts:
3 left
💡 Hint
Common Mistakes
Using FastAPI instead of BaseModel
Returning item.price instead of item.name