前言
FastAPI越来越流行,好处多多,慢慢会成为Web后端的主流。我初步尝试了一下。
一、基本代码

代码如下:
from fastapi import FastAPI
创建应用实例
app = FastAPI()
========== 定义一个路由 ==========
@app.get("/") # GET 请求
def read_root():
return {"message": "Hello, FastAPI!"}
@app.get("/hello/{name}") # 动态路由参数
def say_hello(name: str):
return {"message": f"你好,{name}!欢迎使用 FastAPI!"}
@app.post("/sum") # POST 请求示例
def calc_sum(data: dict):
a = data.get("a", 0)
b = data.get("b", 0)
return {"a": a, "b": b, "sum": a + b}
二、启动运行


三、访问效果



四、Post请求示例



输入参数:

运行结果如下:
