Consider this FastAPI endpoint that returns a user by ID from a dictionary.
from fastapi import FastAPI, HTTPException
app = FastAPI()
users = {1: "Alice", 2: "Bob"}
@app.get("/users/{user_id}")
async def read_user(user_id: int):
if user_id in users:
return {"user": users[user_id]}
raise HTTPException(status_code=404, detail="User not found")What will be the JSON response when a client requests /users/2?
from fastapi import FastAPI, HTTPException app = FastAPI() users = {1: "Alice", 2: "Bob"} @app.get("/users/{user_id}") async def read_user(user_id: int): if user_id in users: return {"user": users[user_id]} raise HTTPException(status_code=404, detail="User not found")
Check the dictionary key used to fetch the user.
The endpoint checks if user_id is in the users dictionary. For user_id=2, it returns {"user": "Bob"}.
You want to create a POST endpoint in FastAPI that accepts a JSON body with a name field and returns the created item with an id.
Which code snippet is syntactically correct and follows FastAPI patterns?
Think about how FastAPI extracts data from the request body.
Option B uses Body(...) to declare that name comes from the JSON body, which is the correct FastAPI pattern for POST data.
Examine this FastAPI PUT endpoint meant to update a user's name:
from fastapi import FastAPI
app = FastAPI()
users = {1: "Alice", 2: "Bob"}
@app.put("/users/{user_id}")
async def update_user(user_id: int, name: str):
if user_id in users:
users[user_id] = name
return {"user": users[user_id]}
return {"error": "User not found"}When a client sends a PUT request with JSON body {"name": "Charlie"}, the server responds with 422 error. Why?
from fastapi import FastAPI app = FastAPI() users = {1: "Alice", 2: "Bob"} @app.put("/users/{user_id}") async def update_user(user_id: int, name: str): if user_id in users: users[user_id] = name return {"user": users[user_id]} return {"error": "User not found"}
Check how FastAPI matches function parameters to request data.
FastAPI treats simple parameters like name: str as query parameters by default. Since the client sends JSON body, FastAPI cannot find 'name' in query parameters, causing 422 error.
Given this FastAPI app:
from fastapi import FastAPI, HTTPException
app = FastAPI()
users = {1: "Alice", 2: "Bob", 3: "Charlie"}
@app.delete("/users/{user_id}")
async def delete_user(user_id: int):
if user_id in users:
del users[user_id]
return {"message": "Deleted"}
raise HTTPException(status_code=404, detail="User not found")If a client sends DELETE requests to /users/2 then /users/3, what is the content of users afterwards?
from fastapi import FastAPI, HTTPException app = FastAPI() users = {1: "Alice", 2: "Bob", 3: "Charlie"} @app.delete("/users/{user_id}") async def delete_user(user_id: int): if user_id in users: del users[user_id] return {"message": "Deleted"} raise HTTPException(status_code=404, detail="User not found")
Think about what happens when you delete keys from a dictionary.
Deleting user 2 removes key 2, deleting user 3 removes key 3, leaving only key 1 with "Alice".
Idempotency means that making the same request multiple times has the same effect as making it once.
Which FastAPI CRUD method is idempotent by design?
Think about which HTTP methods do not change server state.
GET requests are idempotent because they only retrieve data without changing it. POST creates new resources each time, DELETE changes state and may error if repeated, PATCH changes state and may differ each time.