FastAPI的最简单实例

前言

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请求示例

输入参数:

运行结果如下:

相关推荐
还是鼠鼠5 小时前
AI掘金头条新闻系统 (Toutiao News)-获取用户信息
后端·python·mysql·fastapi·web
我叫张小白。8 小时前
基于Redis与FastAPI的分布式共享会话体系
数据库·redis·分布式·缓存·中间件·fastapi·依赖注入
我叫张小白。19 小时前
FastAPI 介绍和入门核心知识点
fastapi
智研数智工坊1 天前
FastAPI+uv+Jinja2+Nuitka 通用Web桌面框架搭建教程|从零搭建可打包迭代的Python开发底座
python·fastapi·uv·nuitka·jinja2·桌面应用开发
alwaysrun1 天前
python之异步高性能Web框架 FastAPI
python·fastapi·web·路由·pydantic
dinl_vin2 天前
FastAPI 系列 ·(十二):生产部署——Docker + 配置管理(系列完结)
docker·容器·fastapi
L_cl2 天前
大模型应用开发 9.FastAPI ① 请求与响应
python·fastapi
是你就无限6152 天前
FastAPI 核心技术与实战
python·fastapi
我叫张小白。2 天前
FastAPI 进阶:ORM(SQLAlchemy 异步)
fastapi
dinl_vin2 天前
FastAPI 系列 ·(十一):ClickHouse 集成——大数据查询实战
大数据·clickhouse·fastapi