随笔分类 - FastAPI
表单数据、请求文件
摘要:FastAPI 表单数据 需要接收的不是JSON,而是表单字段时,可以使用Form。 使用表单时,请先安装python-multipart, pip install python-multipart 定义form参数 创建表单参数的方式与Body和Query一样: from fastapi impo
阅读全文
响应模型
摘要:FastAPI 响应模型 使用response_model参数,即可在以下路径参数中声明响应模型: @app.get() @app.put() @app.post() @app.delete() from typing import List, Optional from fastapi impor
阅读全文
Cookie参数、Header参数
摘要:FastAPI Cookie参数 定义Cookie参数与定义Query和Path参数一样。 第一个值是默认值,还可以传递所有验证参数或注释参数: from typing import Optional from fastapi import Cookie, FastAPI app = FastAPI
阅读全文
请求体
摘要:FastAPI 请求体 多个参数 混用Path、Query和请求体参数 from fastapi import FastAPI, Path from typing import Optional from pydantic import BaseModel app = FastAPI() class
阅读全文
路径参数和数值校验
摘要:FastAPI 路径参数和数值校验 除了可以为Query查询参数声明校验和元数据,还可以为Path路径参数声明相同类型的校验和元数据。 声明元数据 可以声明与Query相同的所有参数。 例如:为路径参数item_id声明title元数据的值时,可以输入: from typing import Opt
阅读全文
路径参数、查询参数
摘要:FastAPI 路径参数 FastAPI 使用python 字符串格式化语法声明路径参数(变量)。 from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id
阅读全文
安装、小demo、基本的步骤
摘要:FastAPI 有两个依赖支持: Starlette负责网络 Pydantic负责数据 安装: 安装命令 pip install fastapi FastAPI 还需要ASGI服务器,生产环境下可以使用Uvicorn pip install uvicorn[standard] 也可以使用以下
阅读全文