gpt4 book ai didi

python - Fastapi在浏览器中访问URL时返回404

转载 作者:行者123 更新时间:2023-12-03 08:18:43 90 4
gpt4 key购买 nike

我正在学习 fastapi 并创建了以下示例应用程序:

from fastapi import FastAPI
import uvicorn
app = FastAPI()

@app.get("/hello")
async def hello_world():
return {"message": "hello_world"}

if __name__== "__main__":
uvicorn.run(app, host="127.0.0.1", port=8080)

服务器启动正常,但是当我在浏览器中测试 url 时,出现以下错误:

{"detail": "Not Found"}

日志中出现此错误:

"GET / HTTP /" 404 Not Found

我注意到另一个奇怪的问题,当我犯一些错误时,它没有检测到错误并且仍然启动服务器。例如,如果我更改函数如下:

@app.get("/hello")
async def hello_world():
print (sample)
return {"message": "hello_world"}

它应该抛出错误:

NameError: "sample" not defined

但它仍在启动服务器。任何建议都会有帮助。

最佳答案

问题1:不起作用。您为路径 /hello 编写了一个处理程序。但您正在测试未配置的路径 /

问题 2:您没有定义变量 sample

关于python - Fastapi在浏览器中访问URL时返回404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68532627/

90 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com