FastApi error assert default is, "Path parameters cannot have a default value"
Error when setting up description for dynamic path in fastapi
Error
File "C:\Users\Maesterzak\Desktop\Codes\web2\FastApi\Practice2\venv\lib\site-packages\fastapi\param_functions.py", line 306, in Path
return params.Path(
File "C:\Users\Maesterzak\Desktop\Codes\web2\FastApi\Practice2\venv\lib\site-packages\fastapi\params.py", line 182, in __init__
assert default is ..., "Path parameters cannot have a default value"
AssertionError: Path parameters cannot have a default value
App.py
from fastapi import FastAPI, Path
app = FastAPI()
todos = {
1:{
'title':'First Todo Item',
'description': 'Make hay while the sun shines'
},
2:{
'title':'Second Todo Item',
'description': 'Make hay while the sun shines'
},
3:{
'title':'Third Todo Item',
'description': 'Make hay while the sun shines'
},
4:{
'title':'Fourth Todo Item',
'description': 'Make hay while the sun shines'
},
}
@app.get("/")
def index():
items = todos
return {"message": "success", "data": items}
@app.get("/{id}")
def todoItem(id:int = Path(None, description="Id of todo item")):
try:
item = todos[id]
if item != None:
return {"message": "success", "data": item}
else:
return {"message":"Not found"}
except Exception as e:
return {"message":f"Error Occurred: {e}"}
Admin
2023-12-10
Remove None from the default parameter section and leave only description as shown below
@app.get("/{id}")
def todoItem(id:int = Path(description="Id of todo item")):
try:
item = todos[id]
if item != None:
return {"message": "success", "data": item}
else:
return {"message":"Not found"}
except Exception as e:
return {"message":f"Error Occurred: {e}"}
Similar Content
- Printing all even or odd numbers between a maximum and minimum value using python
- Django urls.py error on migration
- ckeditor.js error exportpdf-no-token-url in django
- How to add clipboard functionality to Django CkEditor Code Snippets
- Part 5: Building a User-Friendly Todo App with Flask - Step-by-Step Guide to Update Todo and Category Items via PATCH Requests
Privacy Policy
By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.