Challenge - 5 Problems
FastAPI Path Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this GET path operation?
Consider this FastAPI code snippet. What will be the response when a GET request is made to
/items/42?FastAPI
from fastapi import FastAPI app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: int): return {"item_id": item_id, "message": f"Item {item_id} found"}
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type.
✗ Incorrect
The path parameter
item_id is declared as an integer, so FastAPI converts the string '42' from the URL to the integer 42. The function returns a dictionary with the integer and a message.📝 Syntax
intermediate2:00remaining
Which option correctly defines a POST path operation to create an item?
You want to create a POST path operation in FastAPI that accepts an
item_id as a path parameter and returns a confirmation message. Which code snippet is correct?Attempts:
2 left
💡 Hint
Check the HTTP method decorator and parameter types.
✗ Incorrect
Option A uses @app.post with the correct path including
{item_id} and declares item_id as an int with async function. Option A uses GET instead of POST. Option A misses the path parameter in the route. Option A misses the async keyword.❓ state_output
advanced2:00remaining
What is the value of the item after this PUT request?
Given this FastAPI app, what will be the value of
items[3] after a PUT request to /items/3 with JSON body {"name": "NewName"}?FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() items = {3: {"name": "OldName"}} class Item(BaseModel): name: str @app.put('/items/{item_id}') async def update_item(item_id: int, item: Item): items[item_id] = item.dict() return items[item_id]
Attempts:
2 left
💡 Hint
The PUT operation replaces the item with the new data.
✗ Incorrect
The PUT path operation updates the dictionary entry for key 3 with the new item data from the request body. So items[3] becomes {"name": "NewName"}.
🔧 Debug
advanced2:00remaining
Which option causes a runtime error when deleting an item?
Given this FastAPI app, which DELETE path operation code will cause a runtime error if the item does not exist?
FastAPI
from fastapi import FastAPI app = FastAPI() items = {1: "a", 2: "b"}
Attempts:
2 left
💡 Hint
Deleting a non-existent key with del causes an error.
✗ Incorrect
Option D uses del without checking if the key exists, so deleting a missing key raises KeyError. Options A, B, and D handle missing keys safely.
🧠 Conceptual
expert2:00remaining
Which HTTP method is idempotent and safe to call multiple times without changing the resource state?
In RESTful APIs using FastAPI, which HTTP method among GET, POST, PUT, DELETE is idempotent and safe to call multiple times without side effects?
Attempts:
2 left
💡 Hint
Idempotent means the same request can be repeated without changing the result.
✗ Incorrect
GET is safe and idempotent because it only retrieves data without changing server state. PUT and DELETE are idempotent but can change state. POST is neither safe nor idempotent.