Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the FastAPI class.
FastAPI
from fastapi import [1] app = [1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
Forgetting to create the app instance.
✗ Incorrect
The FastAPI class is imported to create the app instance.
2fill in blank
mediumComplete the code to define a Pydantic model for user input validation.
FastAPI
from pydantic import BaseModel class User([1]): name: str age: int
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Model or Schema which are not Pydantic base classes.
Forgetting to inherit from any class.
✗ Incorrect
Pydantic models inherit from BaseModel to enable validation.
3fill in blank
hardFix the error in the route decorator to accept POST requests.
FastAPI
@app.[1]("/users") async def create_user(user: User): return user
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for data creation.
Using PUT or DELETE which are for update or removal.
✗ Incorrect
The POST method is used to send data to the server for creation.
4fill in blank
hardFill both blanks to validate user input and return a success message.
FastAPI
from fastapi import [1] @app.post("/users") async def create_user(user: [2]): return {"message": f"User {user.name} created"}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request or Response instead of User model for input.
Not importing FastAPI.
✗ Incorrect
FastAPI is imported to create the app, and User is the Pydantic model for validation.
5fill in blank
hardFill all three blanks to create a validated POST endpoint that returns the user's age doubled.
FastAPI
from fastapi import [1] from pydantic import BaseModel app = [2]() class User(BaseModel): name: str age: int @app.post("/double-age") async def double_age(user: [3]): return {"double_age": user.age * 2}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BaseModel instead of User for the parameter.
Not creating the app instance correctly.
✗ Incorrect
FastAPI is imported and used to create the app instance. The User model validates input.