Challenge - 5 Problems
FastAPI Request Body Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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."}
Attempts:
2 left
💡 Hint
Look at how the endpoint returns a message using the user data from the request body.
✗ Incorrect
The endpoint receives a User model instance from the request body and returns a message string using the name and age fields. So the output JSON contains the message with the values inserted.
📝 Syntax
intermediate2: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
Attempts:
2 left
💡 Hint
Pydantic models are automatically treated as request body parameters when used as type annotations.
✗ Incorrect
In FastAPI, declaring a function parameter with a Pydantic model type annotation (e.g., item: Item) automatically expects the data in the request body and validates it as Item.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Check the type of the 'id' field in the Pydantic model and the input value.
✗ Incorrect
Pydantic validates the input types strictly. The 'id' field expects an integer, but the input provides a string 'abc', so validation fails and FastAPI returns a 422 error.
❓ state_output
advanced2: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}
Attempts:
2 left
💡 Hint
Pydantic requires all fields without defaults to be present in the request body.
✗ Incorrect
Since 'name' and 'price' are required fields without default values, sending an empty JSON causes validation to fail and FastAPI returns a 422 error.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI request body declaration is TRUE?
Select the correct statement about how FastAPI handles request body declarations with Pydantic models.
Attempts:
2 left
💡 Hint
Think about how FastAPI integrates with Pydantic for request validation.
✗ Incorrect
FastAPI automatically reads the JSON request body, converts it into the declared Pydantic model, and validates it. You do not need to manually parse JSON or always use Body(...). Nested models are supported.