0
@router.get('/{item_id}',response_model=Item,status_code=status.HTTP_200_OK)
def get_item(item_id:int):
 item = db.query(models.Item).filter(models.Item.id==item_id).first()
 return item

@router.get('/{category}',response_model=List[Item],status_code=status.HTTP_200_OK)
def get_items_by_category(category:str):
    items = db.query(models.Item).where(models.Item.category==category).all()
    return items

I created basic API's with FastAPI and also I wanna create categorical query with upper code. But when I try to run category query at Swagger, it says

{
  "detail": [
    {
      "loc": [
        "path",
        "item_id"
      ],
      "msg": "value is not a valid integer",
      "type": "type_error.integer"
    }
  ]
}

I wanna both Id(int) query and Categorical(str) query at same "/items" path. How can I do that? (Other files and code is correct. When I change the category parth, it works but I wanna do this at same path as I say).

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Hakan
  • 21
  • 2
  • 1
    When you create these two routes, which have the same path, Fastapi only considers the first one. Therefore every time you send a request, it responds with the one which is written earlier. So, in your code it tries to respond all the requests with the first one. This is not recommended to have two routes with the same name. Try to change one of them with a prefix. – Daniel Mar 05 '23 at 19:35
  • What @Daniel said. Such paths are [considered duplicate](https://stackoverflow.com/a/35494374/113116) by the OpenAPI Specification (which FastAPI implements), and so are not allowed. You must make the paths unique. – Helen Mar 06 '23 at 08:48

0 Answers0