0
0
FastAPIframework~10 mins

Why validation prevents bad data 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 import the FastAPI class.

FastAPI
from fastapi import [1]
app = [1]()
Drag options to blanks, or click blank then click option'
AResponse
BRequest
CFastAPI
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
Forgetting to create the app instance.
2fill in blank
medium

Complete 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'
ABaseModel
BModel
CSchema
DValidator
Attempts:
3 left
💡 Hint
Common Mistakes
Using Model or Schema which are not Pydantic base classes.
Forgetting to inherit from any class.
3fill in blank
hard

Fix 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'
Adelete
Bget
Cput
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for data creation.
Using PUT or DELETE which are for update or removal.
4fill in blank
hard

Fill 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'
AFastAPI
BUser
CRequest
DResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request or Response instead of User model for input.
Not importing FastAPI.
5fill in blank
hard

Fill 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'
AFastAPI
CUser
DBaseModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using BaseModel instead of User for the parameter.
Not creating the app instance correctly.