Challenge - 5 Problems
Structured Data Mastery in FastAPI
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why do request bodies carry structured data in FastAPI?
In FastAPI, why is it important that request bodies carry structured data like JSON objects instead of plain text?
Attempts:
2 left
💡 Hint
Think about how FastAPI uses data models to handle incoming data.
✗ Incorrect
FastAPI uses structured data like JSON to automatically check if the data matches expected types and shapes. This helps catch errors early and makes it easy to work with the data as Python objects.
❓ component_behavior
intermediate2:00remaining
What happens when FastAPI receives a structured request body?
Consider a FastAPI endpoint expecting a JSON body matching a Pydantic model. What does FastAPI do when it receives the request?
Attempts:
2 left
💡 Hint
Think about how FastAPI uses Pydantic models.
✗ Incorrect
FastAPI automatically parses and validates the JSON body against the Pydantic model, then provides a Python object to the endpoint function for easy use.
📝 Syntax
advanced2:00remaining
Identify the correct FastAPI endpoint to accept structured JSON data
Which FastAPI endpoint code correctly accepts a JSON request body matching the User model and returns the user's name?
FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class User(BaseModel): name: str age: int @app.post("/user") def create_user(user: User): return {"user_name": user.name}
Attempts:
2 left
💡 Hint
Check the decorator and model inheritance.
✗ Incorrect
The code uses @app.post to accept POST requests with JSON matching the User model. The function returns the user's name correctly.
🔧 Debug
advanced2:00remaining
Why does this FastAPI endpoint fail to parse the request body?
Given this FastAPI code, why does sending a JSON body cause a validation error?
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
id: int
description: str
@app.post("/items")
def create_item(item: Item):
return item
Sent JSON body: {"id": "123", "description": "A book"}
Attempts:
2 left
💡 Hint
Check the data types in the JSON vs the model.
✗ Incorrect
The id field is sent as a string "123" but the model expects an int. FastAPI's validation fails because of this type mismatch.
❓ state_output
expert2:00remaining
What is the output of this FastAPI endpoint with nested structured data?
Given this FastAPI code, what will be the JSON response when posting the shown JSON body?
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Address(BaseModel):
city: str
zip_code: str
class User(BaseModel):
name: str
address: Address
@app.post("/users")
def create_user(user: User):
return {"city": user.address.city, "name": user.name}
JSON body sent:
{"name": "Alice", "address": {"city": "Wonderland", "zip_code": "12345"}}
Attempts:
2 left
💡 Hint
Look at how nested models are accessed in the return statement.
✗ Incorrect
The endpoint returns the city from the nested address and the user's name as provided in the JSON body.