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}"}

 

message profile
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}"}

 

Add Message

Click on the button below to add a new message to this thread

Tags

#python #fastapi

Thread detail

Satus: Open
Messages: 1Started: 2023-12-10
loading..

DEVMAESTERS

Newsletter

Services

Frontend Development |Backend Development |Full Website Development |Bootstrap Website upgrades | Website Debbugging | Website Hosting & deployment

Contact

Interested in hiring me or collaborating with me on a project, click on any of the links below to get my social media handle

Or contact me via Tel: (+234)-806-225-7480 | Email: abubakarzakari1703@gmail.com

Copywright@devmaesters.com
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.