Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import FastAPI and create an app instance.
FastAPI
from fastapi import [1] app = [1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong class like Request or Response.
Forgetting to create the app instance.
✗ Incorrect
You need to import FastAPI and create an instance of it to start your app.
2fill in blank
mediumComplete the code to define a GET endpoint that returns a welcome message.
FastAPI
@app.[1]("/") async def read_root(): return {"message": "Welcome to FastAPI!"}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for a read endpoint.
Forgetting the async keyword.
✗ Incorrect
The GET method is used to read or retrieve data from the server.
3fill in blank
hardFix the error in the POST endpoint to accept JSON data with a 'name' field.
FastAPI
from pydantic import BaseModel class Item(BaseModel): name: str @app.post("/items/") async def create_item(item: [1]): return {"item_name": item.name}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a plain dict instead of the Pydantic model.
Using wrong types like str or int.
✗ Incorrect
The parameter type should be the Pydantic model Item to parse JSON data automatically.
4fill in blank
hardFill both blanks to update an item by its ID using PUT method.
FastAPI
@app.[1]("/items/{item_id}") async def update_item(item_id: int, item: [2]): return {"item_id": item_id, "updated_name": item.name}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of PUT for updates.
Using wrong parameter types.
✗ Incorrect
Use put decorator for updates and the Item model to receive data.
5fill in blank
hardFill all three blanks to delete an item by its ID and return a confirmation message.
FastAPI
@app.[1]("/items/{item_id}") async def delete_item(item_id: [2]): return {"message": f"Item [3] deleted"}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong HTTP method like GET or POST.
Using string type for item_id instead of int.
Not including the item_id in the confirmation message.
✗ Incorrect
Use delete decorator, int type for ID, and include item_id in the message.