OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why does does my FastAPI route return 404?

  • Thread starter Thread starter MisutoWolf
  • Start date Start date
M

MisutoWolf

Guest
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:

Code:
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.
<p>Working on a small (learning) project using FastAPI, running the code with Uvicorn.</p>
<p>Any time I try to access a route that isn't <code>/</code>, I get a 404 error, and I'm not sure what's causing it.</p>
<p>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 <code>{ "detail": "Not Found" }</code> and the code for the <code>/cards</code> route never seems to even get executed:</p>
<pre><code>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"
</code></pre>
<p>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?</p>
<p>Thanks in advance.</p>
 

Latest posts

Top