0
0
FastAPIframework~20 mins

Why request bodies carry structured data in FastAPI - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structured Data Mastery in FastAPI
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2: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?
ABecause structured data allows FastAPI to automatically validate and convert the data into Python objects.
BBecause plain text is faster to process but less secure than structured data.
CBecause FastAPI only accepts XML data in request bodies, not JSON or plain text.
DBecause structured data is required to make the request body smaller in size.
Attempts:
2 left
💡 Hint
Think about how FastAPI uses data models to handle incoming data.
component_behavior
intermediate
2: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?
AIt rejects the request if the JSON is valid but does not match the model exactly.
BIt stores the JSON as a plain string and passes it to the endpoint without validation.
CIt parses the JSON, validates it against the model, and passes a Python object to the endpoint function.
DIt converts the JSON into XML before passing it to the endpoint.
Attempts:
2 left
💡 Hint
Think about how FastAPI uses Pydantic models.
📝 Syntax
advanced
2: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}
AThe code is missing @app.get decorator instead of @app.post.
BThe code is correct and will accept JSON matching User and return the name.
CThe User model should inherit from object, not BaseModel.
DThe function should return user.age instead of user.name.
Attempts:
2 left
💡 Hint
Check the decorator and model inheritance.
🔧 Debug
advanced
2: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"}
ABecause the id field is a string in JSON but expected as int in the model, causing validation error.
BBecause the description field is missing in the JSON body.
CBecause FastAPI requires XML format, not JSON.
DBecause the endpoint uses @app.get instead of @app.post.
Attempts:
2 left
💡 Hint
Check the data types in the JSON vs the model.
state_output
expert
2: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"}}
A{"city": null, "name": "Alice"}
B{"city": "12345", "name": "Alice"}
C{"city": "Wonderland", "name": null}
D{"city": "Wonderland", "name": "Alice"}
Attempts:
2 left
💡 Hint
Look at how nested models are accessed in the return statement.