前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >fastapi 模式的额外信息,示例 / Cookie参数 / Header参数

fastapi 模式的额外信息,示例 / Cookie参数 / Header参数

作者头像
Michael阿明
发布2022-01-07 11:37:45
3880
发布2022-01-07 11:37:45
举报

文章目录

learn from https://fastapi.tiangolo.com/zh/tutorial/schema-extra-example/

添加一个将在文档中显示的 example

1. Pydantic schema_extra

代码语言:javascript
复制
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None

    class Config:
        schema_extra = {
            "example": {
                "name": "michael",
                "description": "a learner",
                "price": 100.0, 
                "tax": 0.1
            }
        }

@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    res = {"item_id": item_id, "item": item}
    return res
在这里插入图片描述
在这里插入图片描述

来看看文档:http://127.0.0.1:8000/docs#/default/update_item_items__item_id__put

在这里插入图片描述
在这里插入图片描述

其中: class Config: schema_extra = { “example”: {

加黑的字符,大小写必须完全一致,应该是内置的字段,否则无法显示例子

2. Field 的附加参数

  • Field(None, example=xxx)
代码语言:javascript
复制
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI()


class Item(BaseModel):
    name: str = Field(..., example="michael")
    description: Optional[str] = Field(None, example="handsome")
    price: float = Field(..., example=34.5)
    tax: Optional[float] = Field(None, example=0.1)


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    res = {"item_id": item_id, "item": item}
    return res
在这里插入图片描述
在这里插入图片描述

3. Body 额外参数

可以通过传递额外信息给 Field 同样的方式操作Path, Query, Body

代码语言:javascript
复制
from typing import Optional
from fastapi import FastAPI, Body
from pydantic import BaseModel, Field
app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None


@app.put("/items/{item_id}")
async def update_item(
    item_id: int, 
    item: Item = Body(
        ...,
        example = {  # 加入 example 参数
            "name": "michael",
            "description": "a learner",
            "price": 100.1, 
            "tax": 0.1
        }
    )
):
    res = {"item_id": item_id, "item": item}
    return res

4. Cookie 参数

声明 Cookie 参数的结构与声明 Query 参数和 Path 参数时相同。

第一个值是参数的默认值,同时也可以传递所有验证参数或注释参数,来校验参数

你需要使用 Cookie 来声明 cookie 参数,否则 参数将会被解释为 查询参数

代码语言:javascript
复制
from typing import Optional
from fastapi import Cookie, FastAPI

app = FastAPI()
@app.get("/items/")
async def read_items(ads_id: Optional[str] = Cookie(None)):
    return {"ads_id": ads_id} 

使用 postman 测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5. Header 参数

大多数标准的 headers 用 "连字符" 分隔,也称为 "减号" (-)

但是像 user-agent 这样的变量在Python中是无效的。

因此, 默认情况下, Header 将把参数名称的字符从下划线 (_) 转换为连字符 (-) 来提取并记录 headers

  • 如果需要禁用 下划线到连字符 的自动转换,设置 Header 的参数 convert_underscores 为 False
  • 注意:一些 HTTP 代理和服务器不允许使用带有下划线的 headers
代码语言:javascript
复制
from typing import Optional
from fastapi import Cookie, FastAPI, Header

app = FastAPI()
@app.get("/items/")
async def read_items(my_agent: Optional[str] = Header(None)):
    return {"my_agent": my_agent}    
在这里插入图片描述
在这里插入图片描述
  • 禁用下划线自动转换 strange_header: Optional[str] = Header(None, convert_underscores=False)
代码语言:javascript
复制
from typing import Optional
from fastapi import Cookie, FastAPI, Header

app = FastAPI()
@app.get("/items/")
async def read_items(my_agent: Optional[str] = Header(None, convert_underscores=False)):
    return {"my_agent": my_agent} 
在这里插入图片描述
在这里插入图片描述

5.1 重复的 headers

可以通过一个Python list 的形式获得 重复header 的 所有值

代码语言:javascript
复制
from typing import Optional, List
from fastapi import Cookie, FastAPI, Header

app = FastAPI()
@app.get("/items/")
async def read_items(x_token: Optional[List[str]] = Header(None)):
    return {"x_token value:": x_token} 
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-10-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. Pydantic schema_extra
  • 2. Field 的附加参数
  • 3. Body 额外参数
  • 4. Cookie 参数
  • 5. Header 参数
    • 5.1 重复的 headers
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档