Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

python - FastAPI with uvicorn getting 404 Not Found error

I'm trying (failing) to set up a simple FastAPI project and run it with uvicorn. This is my code:

from fastapi import FastAPI

app = FastAPI()

app.get('/')

def hello_world():
    return{'hello':'world'}

app.get('/abc')

def abc_test():
    return{'hello':'abc'}

This is what I run from the terminal:

PS C:UsersadminDesktopSelf pace studyPythonDevday 14> uvicorn server2:app   
INFO:     Started server process [3808]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:60391 - "GET / HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:60391 - "GET /favicon.ico HTTP/1.1" 404 Not Found

As you see, I get a 404 Not found. What could be the reason? Some network-related stuff, possibly firewall/vpn blocking this connection or something else? I'm new to this. Thanks in advance!

question from:https://stackoverflow.com/questions/65599686/fastapi-with-uvicorn-getting-404-not-found-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use a decorator like this: @app.get('/'). Take a look at the FastAPI Docs.

Additionally, take a look at how decorators work in general to get a better idea of how things work behind the scenes.

Some resources for you:

python docs

one of many articles I was able to find

another SO question


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...