Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the FastAPI class.
FastAPI
from fastapi import [1] app = FastAPI()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
✗ Incorrect
The FastAPI class is imported to create the app instance which handles routing.
2fill in blank
mediumComplete the code to define a GET endpoint at path '/'.
FastAPI
@app.[1]("/") def read_root(): return {"message": "Hello World"}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using post or put instead of get for a read endpoint.
✗ Incorrect
The GET method defines an endpoint that responds to HTTP GET requests.
3fill in blank
hardFix the error in the route decorator to correctly organize the endpoint.
FastAPI
@app.[1]("/items/{item_id}") def read_item(item_id: int): return {"item_id": item_id}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using post or put which are for creating or updating data.
✗ Incorrect
The GET method is appropriate for reading an item by its ID in the URL path.
4fill in blank
hardFill both blanks to create a POST endpoint that accepts JSON data.
FastAPI
@app.[1]("/users") async def create_user(user: [2]): return user
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST or wrong parameter type.
✗ Incorrect
POST is used to create resources. The user parameter is typed as dict to accept JSON data.
5fill in blank
hardFill all three blanks to organize endpoints with path and query parameters.
FastAPI
@app.[1]("/search") async def search_items(q: [2] = None, limit: [3] = 10): return {"query": q, "limit": limit}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST for search or wrong parameter types.
✗ Incorrect
GET method is used for searching. Query parameter q is a string, limit is an integer with default 10.