Working on a small (learning) project using FastAPI, running the code with Uvicorn.
Any time I try to access a route that isn’t /
, I get a 404 error, and I’m not sure what’s causing it.
I’ve got this small bit of code, just loads a JSON file and then it should search through the list and return a dict if found…otherwise 404. But I get { "detail": "Not Found" }
and the code for the /cards
route never seems to even get executed:
from fastapi import FastAPI, HTTPException
import json
import os
# FastAPI Test
app = FastAPI()
cwd = os.getcwd()
card_filename = os.path.join(cwd, 'data', 'cards.json')
with open(card_filename, 'r') as card_file:
cards = json.load(card_file)
print(f"Loaded card data, found {len(cards)} cards...")
@app.get("/card/{name}")
async def get_card(name: str):
print(f"Got request for card: {name}. Searching now...")
card = next((x for x in cards if x[name].lower() == name.lower()), None)
if card is None:
return card
else:
raise HTTPException(status_code=404, detail=f"Card not found with name: {name}")
@app.get('/')
async def root():
return "Hello, World"
What would be causing this? I figured maybe it was the order of routes, so I made sure to put the one I’m having issues with first, but that’s having no effect?
Thanks in advance.