Complete the code to import FastAPI and create an app instance.
from fastapi import [1] app = [1]()
You need to import FastAPI and create an instance of it to start your app.
Complete the code to define a GET endpoint that returns a welcome message.
@app.[1]("/") async def read_root(): return {"message": "Welcome to FastAPI!"}
The GET method is used to read or retrieve data from the server.
Fix the error in the POST endpoint to accept JSON data with a 'name' field.
from pydantic import BaseModel class Item(BaseModel): name: str @app.post("/items/") async def create_item(item: [1]): return {"item_name": item.name}
The parameter type should be the Pydantic model Item to parse JSON data automatically.
Fill both blanks to update an item by its ID using PUT method.
@app.[1]("/items/{item_id}") async def update_item(item_id: int, item: [2]): return {"item_id": item_id, "updated_name": item.name}
Use put decorator for updates and the Item model to receive data.
Fill all three blanks to delete an item by its ID and return a confirmation message.
@app.[1]("/items/{item_id}") async def delete_item(item_id: [2]): return {"message": f"Item [3] deleted"}
Use delete decorator, int type for ID, and include item_id in the message.
