Complete the code to declare a path parameter as an integer in FastAPI.
from fastapi import FastAPI app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: [1]): return {"item_id": item_id}
The path parameter item_id should be typed as int to ensure FastAPI treats it as an integer.
Complete the code to validate that the path parameter item_id is greater than zero.
from fastapi import FastAPI, Path app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: int = Path(..., gt=[1])): return {"item_id": item_id}
ge=0 which allows zero, not strictly greater.gt to a negative number.The gt=0 argument ensures item_id must be greater than zero.
Fix the error in the code by specifying the correct type for the path parameter user_id.
from fastapi import FastAPI app = FastAPI() @app.get('/users/{user_id}') async def get_user(user_id: [1]): return {"user_id": user_id}
str when numeric IDs are expected.float which is not typical for IDs.The user_id should be an int to match expected numeric user IDs.
Fill both blanks to declare a path parameter item_id as an integer and validate it to be between 1 and 100.
from fastapi import FastAPI, Path app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: [1] = Path(..., ge=[2], le=100)): return {"item_id": item_id}
str instead of int.ge to 0 instead of 1.Use int for the type and ge=1 to ensure the value is at least 1.
Fill all three blanks to declare a path parameter user_id as an integer, validate it to be positive, and add a description.
from fastapi import FastAPI, Path app = FastAPI() @app.get('/users/{user_id}') async def get_user(user_id: [1] = Path(..., gt=[2], description=[3])): return {"user_id": user_id}
str instead of int.Use int type, gt=0 to ensure positive values, and add a descriptive string for documentation.