0
0
FastAPIframework~20 mins

Request body declaration in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Request Body Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when sending a POST request with JSON {"name": "Alice", "age": 30}?
Consider this FastAPI endpoint that declares a request body using a Pydantic model. What will the response be when the client sends the JSON {"name": "Alice", "age": 30}?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    name: str
    age: int

@app.post("/user")
async def create_user(user: User):
    return {"message": f"User {user.name} is {user.age} years old."}
A{"message": "User is created."}
B{"message": "User Alice is 30 years old."}
C{"name": "Alice", "age": 30}
D422 Unprocessable Entity error
Attempts:
2 left
💡 Hint
Look at how the endpoint returns a message using the user data from the request body.
📝 Syntax
intermediate
2:00remaining
Which option correctly declares a request body parameter in FastAPI?
You want to declare a request body parameter named 'item' of type 'Item' in a FastAPI POST endpoint. Which code snippet is syntactically correct?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    id: int
    description: str

@app.post("/items")
async def create_item(???):
    return item
Aitem: Item
Bitem = Body(Item)
Citem: Body(Item)
Ditem: Item = Body(...)
Attempts:
2 left
💡 Hint
Pydantic models are automatically treated as request body parameters when used as type annotations.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error when sending {"id": "abc", "description": "Test"}?
Given the following code, why does sending {"id": "abc", "description": "Test"} in the request body cause a validation error?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    id: int
    description: str

@app.post("/items")
async def create_item(item: Item):
    return item
ABecause 'id' is declared as int but the input provides a string 'abc', causing a type validation error.
BBecause 'description' is missing from the request body.
CBecause the endpoint is missing the Body import from fastapi.
DBecause the endpoint does not return a JSON response.
Attempts:
2 left
💡 Hint
Check the type of the 'id' field in the Pydantic model and the input value.
state_output
advanced
2:00remaining
What is the response when sending an empty JSON {} to this endpoint?
Given this FastAPI endpoint, what will be the response if the client sends an empty JSON object {} in the request body?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

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

@app.post("/products")
async def create_product(product: Product):
    return {"product_name": product.name, "product_price": product.price}
A{"product_name": "", "product_price": 0.0}
B{"product_name": null, "product_price": null}
C422 Unprocessable Entity error due to missing required fields
D200 OK with empty JSON response {}
Attempts:
2 left
💡 Hint
Pydantic requires all fields without defaults to be present in the request body.
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI request body declaration is TRUE?
Select the correct statement about how FastAPI handles request body declarations with Pydantic models.
AFastAPI does not support nested Pydantic models in request bodies.
BYou must manually parse JSON request bodies using json.loads before passing to Pydantic models.
CRequest body parameters must always be declared with Body(...) even if using Pydantic models.
DFastAPI automatically parses and validates JSON request bodies into Pydantic models when declared as function parameters.
Attempts:
2 left
💡 Hint
Think about how FastAPI integrates with Pydantic for request validation.