0
0
FastAPIframework~20 mins

Path operations (GET, POST, PUT, DELETE) in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Path Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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"}
A404 Not Found
B{"item_id": 42, "message": "Item 42 found"}
C{"item_id": 42}
D{"item_id": "42", "message": "Item 42 found"}
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type.
📝 Syntax
intermediate
2: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?
A
from fastapi import FastAPI
app = FastAPI()

@app.post('/items/{item_id}')
async def create_item(item_id: int):
    return {"message": f"Item {item_id} created"}
B
from fastapi import FastAPI
app = FastAPI()

@app.get('/items/{item_id}')
async def create_item(item_id: int):
    return {"message": f"Item {item_id} created"}
C
from fastapi import FastAPI
app = FastAPI()

@app.post('/items')
async def create_item(item_id: int):
    return {"message": f"Item {item_id} created"}
D
from fastapi import FastAPI
app = FastAPI()

@app.post('/items/{item_id}')
def create_item(item_id: int):
    return {"message": f"Item {item_id} created"}
Attempts:
2 left
💡 Hint
Check the HTTP method decorator and parameter types.
state_output
advanced
2: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]
A{"name": "OldName"}
B{"name": null}
C{"name": "NewName"}
DKeyError
Attempts:
2 left
💡 Hint
The PUT operation replaces the item with the new data.
🔧 Debug
advanced
2: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"}
A
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    try:
        del items[item_id]
    except KeyError:
        return {"message": "Not found"}
    return {"message": "Deleted"}
B
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    items.pop(item_id, None)
    return {"message": "Deleted"}
C
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    if item_id in items:
        del items[item_id]
    return {"message": "Deleted"}
D
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
    del items[item_id]
    return {"message": "Deleted"}
Attempts:
2 left
💡 Hint
Deleting a non-existent key with del causes an error.
🧠 Conceptual
expert
2: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?
AGET
BPOST
CPUT
DDELETE
Attempts:
2 left
💡 Hint
Idempotent means the same request can be repeated without changing the result.