随笔分类 - FastAPI 框架
FastAPI 是快速构建高效API的现代Web框架,它使用python3.6+,并基于python标准类型提示。
FastAPI 请求表单与文件
摘要:FastAPI支持同时使用File和Form定义文件和表单字段。 from fastapi import FastAPI, File, Form, UploadFile app = FastAPI() @app.post("/files/") async def create_file( file:
阅读全文
FastAPI 请求文件
摘要:File用于定义客户端的上传文件。 from fastapi import FastAPI, File, UploadFile app = FastAPI() @app.post("/files/") async def create_file(file: bytes = File(...)): r
阅读全文
FastAPI 表单数据
摘要:需要接收的不是JSON,而是表单字段时,可以使用Form。 使用表单时,请先安装python-multipart, pip install python-multipart 定义form参数 创建表单参数的方式与Body和Query一样: from fastapi import FastAPI, F
阅读全文
FastAPI 响应模型
摘要:使用response_model参数,即可在以下路径参数中声明响应模型: @app.get() @app.put() @app.post() @app.delete() from typing import List, Optional from fastapi import FastAPI fro
阅读全文
FastAPI Header参数
摘要:定义Header参数的方式与定义Query、Path、Cookie参数相同。 第一个值是默认值,还可以传递所有验证参数或注释参数: from typing import Optional from fastapi import FastAPI, Header app = FastAPI() @app
阅读全文
FastAPI Cookie参数
摘要:定义Cookie参数与定义Query和Path参数一样。 第一个值是默认值,还可以传递所有验证参数或注释参数: from typing import Optional from fastapi import Cookie, FastAPI app = FastAPI() @app.get("/ite
阅读全文
FastAPI 请求体
摘要:多个参数 混用Path、Query和请求体参数 from fastapi import FastAPI, Path from typing import Optional from pydantic import BaseModel app = FastAPI() class Item(BaseMo
阅读全文
FastAPI 路径参数和数值校验
摘要:除了可以为Query查询参数声明校验和元数据,还可以为Path路径参数声明相同类型的校验和元数据。 声明元数据 可以声明与Query相同的所有参数。 例如:为路径参数item_id声明title元数据的值时,可以输入: from typing import Optional from fastapi
阅读全文
FastAPI 查询参数和字符串校验
摘要:FastAPI允许为参数声明附加信息与校验。 from typing import Optional from fastapi import FastAPI app = FastAPI() @app.get("/items/") async def read_items(q: Optional[st
阅读全文
FastAPI 请求体
摘要:FastAPI使用请求体从客户端向API发送数据,请求体是客户端发送给API的数据,响应体是API发送给客户端的数据。API基本上肯定要发送响应体,但是客户端不一定发送请求体。使用pydantic模型声明请求体。 发送数据使用post(最常用)、put、delete、patch等操作。规范中没有定义
阅读全文
FastAPI 查询参数
摘要:声明的参数不是路径参数时,路径操作函数会把该参数自动解释为查询参数。 from fastapi import FastAPI app = FastAPI() fake_items_db = [{"item_name": "foo"}, {"item_name": "bar"}, {"item_nam
阅读全文
FastAPI 路径参数
摘要:FastAPI 使用python 字符串格式化语法声明路径参数(变量)。 from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id): return {"i
阅读全文
创建FastAPI的基本步骤
摘要:第一步:导入FastAPI from fastapi import FastAPI #导入FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"} FastAPI是继承了Star
阅读全文
FastAPI 安装、运行、API文档
摘要:FastAPI 有两个依赖支持: Starlette负责网络 Pydantic负责数据 安装: 安装命令 pip install fastapi FastAPI 还需要ASGI服务器,生产环境下可以使用Uvicorn pip install uvicorn[standard] 也可以使用以下命令安装
阅读全文