0
0
FastAPIframework~10 mins

Multiple path parameters 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 the correct FastAPI class.

FastAPI
from fastapi import [1]
app = FastAPI()
Drag options to blanks, or click blank then click option'
AFastAPI
BDepends
CBody
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of FastAPI.
Importing Body instead of FastAPI.
2fill in blank
medium

Complete the code to import the Body function for request body parameters.

FastAPI
from fastapi import FastAPI, [1]
app = FastAPI()
Drag options to blanks, or click blank then click option'
APath
BDepends
CQuery
DBody
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Depends instead of Body.
Importing Query or Path which are for URL parameters.
3fill in blank
hard

Fix the error in the function parameter to correctly use Body for multiple parameters.

FastAPI
from fastapi import FastAPI, Body
app = FastAPI()

@app.post("/items")
async def create_item(name: str = [1], description: str = Body(...)):
    return {"name": name, "description": description}
Drag options to blanks, or click blank then click option'
ABody(...)
BQuery(...)
CPath(...)
DDepends(...)
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query or Path instead of Body for body parameters.
Not using Body(...) to mark required parameters.
4fill in blank
hard

Fill both blanks to declare two required body parameters with descriptions.

FastAPI
from fastapi import FastAPI, Body
app = FastAPI()

@app.post("/users")
async def create_user(username: str = [1], email: str = [2]):
    return {"username": username, "email": email}
Drag options to blanks, or click blank then click option'
ABody(..., description="The user's username")
BQuery(..., description="The user's username")
CBody(..., description="The user's email")
DPath(..., description="The user's email")
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query or Path instead of Body for body parameters.
Omitting the description argument.
5fill in blank
hard

Fill all three blanks to declare optional body parameters with default values and descriptions.

FastAPI
from fastapi import FastAPI, Body
app = FastAPI()

@app.post("/products")
async def create_product(name: str = [1], price: float = [2], description: str = [3]):
    return {"name": name, "price": price, "description": description}
Drag options to blanks, or click blank then click option'
ABody(None, description="Product name")
BBody(0.0, description="Product price")
CBody(None, description="Product description")
DQuery(None)
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query instead of Body for body parameters.
Not providing default values for optional parameters.