Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing other classes like Request instead of FastAPI
Forgetting to create an instance of FastAPI
✗ Incorrect
You need to import FastAPI and create an instance of it to start your app.
2fill in blank
mediumComplete the code to define a simple GET route 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for a read route
Misspelling the decorator name
✗ Incorrect
The @app.get decorator defines a GET HTTP method route.
3fill in blank
hardFix the error in the code to correctly include a router from another module.
FastAPI
from fastapi import FastAPI from routers import users app = FastAPI() app.[1](users.router)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
add_route which is not a FastAPI methodConfusing
mount with include_router✗ Incorrect
Use include_router to add routers from other modules in FastAPI.
4fill in blank
hardFill both blanks to create a modular project structure with routers and models.
FastAPI
from fastapi import FastAPI from [1] import router as user_router from [2] import User app = FastAPI() app.include_router(user_router)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up routers and models imports
Using wrong folder names
✗ Incorrect
Routers are usually in a routers folder, models in a models folder.
5fill in blank
hardFill all three blanks to define a Pydantic model and use it in a POST route.
FastAPI
from fastapi import FastAPI from pydantic import BaseModel class [1](BaseModel): name: str age: int app = FastAPI() @app.post("/users") async def create_user(user: [2]): return {"user_name": user.[3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for model and parameter
Returning wrong attribute from user
✗ Incorrect
The Pydantic model is named User. The POST route accepts a User object and returns the name attribute.