0
0
FastAPIframework~10 mins

Request body declaration 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 declare a request body parameter in FastAPI.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

@app.post("/items/")
async def create_item(item: [1]):
    return item
Drag options to blanks, or click blank then click option'
AItem
BRequest
CBody
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Request' instead of the Pydantic model.
Using 'Body' without importing or proper context.
Using primitive types like 'str' for complex data.
2fill in blank
medium

Complete the code to import the correct function to declare a request body explicitly.

FastAPI
from fastapi import FastAPI, [1]
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    username: str
    email: str

@app.post("/users/")
async def create_user(user: User = [1]()):
    return user
Drag options to blanks, or click blank then click option'
ADepends
BRequest
CBody
DForm
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Request' which is for the entire HTTP request, not just the body.
Using 'Depends' which is for dependency injection.
Using 'Form' which is for form data.
3fill in blank
hard

Fix the error in the code to correctly declare a request body with a default value.

FastAPI
from fastapi import FastAPI, Body
from pydantic import BaseModel

app = FastAPI()

class Product(BaseModel):
    name: str
    price: float

@app.post("/products/")
async def create_product(product: Product = [1]):
    return product
Drag options to blanks, or click blank then click option'
ABody()
BBody(default=...)
CBody(default=Product())
DBody(default=None)
Attempts:
3 left
💡 Hint
Common Mistakes
Using Body(default=None) which makes the body optional.
Using Body() without specifying required or default.
Trying to instantiate the model as default.
4fill in blank
hard

Fill both blanks to declare a request body with an alias and description.

FastAPI
from fastapi import FastAPI, Body
from pydantic import BaseModel

app = FastAPI()

class Order(BaseModel):
    item_id: int
    quantity: int

@app.post("/orders/")
async def create_order(order: Order = Body([1], description=[2])):
    return order
Drag options to blanks, or click blank then click option'
A...
B'Order data'
C'order_data'
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using None instead of ... for required body.
Putting alias string in the first blank instead of description.
Not providing description string.
5fill in blank
hard

Fill all three blanks to declare a request body with an alias, example, and minimum value validation.

FastAPI
from fastapi import FastAPI, Body
from pydantic import BaseModel, Field

app = FastAPI()

class Payment(BaseModel):
    amount: float = Field(..., gt=0)
    method: str

@app.post("/payments/")
async def create_payment(payment: Payment = Body(
    ...,
    alias=[1],
    example=[2],
    description=[3]
)):
    return payment
Drag options to blanks, or click blank then click option'
A'payment_info'
B{"amount": 100.0, "method": "credit_card"}
C"Payment details with positive amount"
D"payment"
Attempts:
3 left
💡 Hint
Common Mistakes
Using alias as a dictionary instead of string.
Not providing example as a dictionary.
Using description as a non-string.