工程目录
app
... __init__.py .
.. users.py
main.py
from fastapi import APIRouter
router = APIRouter()
@router.get('/{id}')
async def users(id):
return {'name':'Bob'+id}
路由注册
from fastapi import FastAPI
import uvicorn
from app.users import router as user_router
app=FastAPI()
app.include_router(user_router,prefix='/users')
if __name__=='__main__':
uvicorn.run(app='main:app',host='127.0.0.1',port=8000,reload=True)
调用测试 GET http://127.0.0.1:8000/users/23
HTTP/1.1 404 Not Found
date: Tue, 10 Jul 2021 10:25:39 GMT
server: uvicorn
content-length: 22
content-type: application/json
Connection: close
{
"detail": "Not Found"
}
这篇博客展示了如何使用FastAPI框架创建一个简单的RESTful API,其中包括定义路由、处理GET请求以及错误处理。示例代码中,创建了一个名为`users`的路由,用于获取带有特定ID的用户信息。然而,在实际运行时,对于ID为23的请求返回了404 Not Found错误。这可能是由于路由未正确配置或者资源不存在导致。
91

被折叠的 条评论
为什么被折叠?



