第二篇链接:2、零基础Apifox测试FastAPI接口入门——POST请求(路由分配、FastAPI的/docs接口文档初识应用、Pydantic数据校验)
一、FastAPI部分
python使用fastapi编写接口内容(文件名:text.py):
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get('/')
def index():
return "APP is running"
@app.get('/product')
def product(page: int = 1, name: str = "Jack"):
return f'page:{page},name:{name}'
if __name__ == "__main__":
uvicorn.run("test:app", reload=True)
其中uvicorn.run("test:app", reload=True)
,此处test为文件名,否则会报错ERROR: Error loading ASGI app. Could not import module "1".
这里是说没有找到1这个文件,因为这个文件的文件名是test:
INFO: Will watch for changes in these directories: ['E:\\mymodel\\llm_code\\study_api']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [31168] using WatchFiles
ERROR: Error loading ASGI app. Could not import module "1".
成功状态:
INFO: Will watch for changes in these directories: ['E:\\mymodel\\llm_code\\study_api']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [22300] using WatchFiles
INFO: Started server process [5884]
INFO: Waiting for application startup.
INFO: Application startup complete.
此时点击其中链接在浏览器中打开可以访问到我们所写的接口内容:
二、Apifox部分
1、安装Apifox
软件管家中安装就行,或者别的也可以。点击加号新建一个接口:
2、创建接口
输入接口路径:/product{page}{name}
,其中{name}
、{page}
代表连接过程中所携带的参数:
3、更改测试环境
将默认测试环境设置为本机的8000端口http://127.0.0.1:8000
:
OK,以上全部保存。
4、发送请求
(此时保持 一、的pycharm中的test是启动的哈)
OVER!!!