Flutter + FastAPI 30天速成计划自用并实践-第1天
·
安装了一个python3.10
安装fastapi,uvicorn,pip install fastapi uvicorn
写了一个hello world的fastapi程序并运行
# fast_main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"hello world"}
@app.get('/items/{item_id}')
def read_item(item_id:int,q:str = None):
return {"item_id": item_id, "q": q}
运行:uvicorn main:app --reload
报错了:Error loading ASGI app. Could not import module "main".
有点懵,查了下ai,说是文件名不对。
修改为:uvicorn fast_main:app --reload
可以了,打开http://127.0.0.1:8000/出现了["hello world"]
访问http://127.0.0.1:8000/items/3,出现了如下返回
{“item_id”:3,“q”:null}
其中3可以理解,q是null,查看下代码
@app.get('/items/{item_id}')
def read_item(item_id:int,q:str = None):
return {"item_id": item_id, "q": q}
q是一个字符串,默认是None,但是反馈缺失null,None和null一样吗,不管了都是空反正,我测试下传入一个字符串’hello’http://127.0.0.1:8000/items/3hello报错无法解析http://127.0.0.1:8000/items/3/hello没有目录
查了下aihttp://127.0.0.1:8000/items/3?q=hello要这样传递q这个参数
得到了{“item_id”:3,“q”:“hello”}
第一天编写文档边装环境写代码2个多小时,完成的很好23.22下班回家–非常的完美
更多推荐



所有评论(0)