python系列:FASTAPI系列12-Cookie值 & FASTAPI系列 13-header参数




一、 FASTAPI系列12-Cookie值

前言

可以像定义 QueryPath 参数一样来定义 Cookie 参数

一、定义Cookie参数

导入cookie参数

from fastapi import Cookie, FastAPI

声明 Cookie 参数的结构与声明 Query 参数Path 参数时相同。
第一个值是参数的默认值,同时也可以传递所有验证参数或注释参数,来校验参数:

from fastapi import Cookie, FastAPI
from typing import Union
from fastapi import Cookie, FastAPI
from typing_extensions import Annotated

app = FastAPI()


@app.get("/users/")
async def get_user(user_id: Annotated[Union[str, None], Cookie()] = None):
    return {"ads_id": user_id}

在读取客户端中Cookie时需要注意,代码中cookie的变量名称必须和客户端cookie中的key值一致!!!

注意:CookiePathQuery是兄弟类,它们都继承自公共的 Param 类,但请记住,当你从 fastapi 导入的 QueryPathCookie 或其他参数声明函数,这些实际上是返回特殊类的函数。你需要使用 Cookie 来声明 cookie 参数,否则参数将会被解释为查询参数。

总结

使用 Cookie 声明 cookie 参数,使用方式与 QueryPath 类似。

二、 FASTAPI系列 13-header参数

前言

使用定义 Query, PathCookie 参数一样的方法定义 Header 参数。

一、声明 Header 参数

导入header

from fastapi import FastAPI, Header

根据使用Cookie 一样的结构定义 header 参数,第一个值是默认值,你可以传递所有的额外验证或注释参数:

from typing import Union
from fastapi import FastAPI, Header
from typing_extensions import Annotated

app = FastAPI()


@app.get("/users/")
async def get_users(user_agent: Annotated[Union[str, None], Header()] = None):
    return {"User-Agent": user_agent}

HeaderPath, QueryCookie 的兄弟类型。它也继承自通用的 Param 类,但是请记得,当你从fastapi导入 Query, Path, Header, 或其他时,实际上导入的是返回特定类型的函数。

为了声明headers, 你需要使用Header, 因为否则参数将被解释为查询参数。

@app.get("/users")  
async def users(  
        user_agent: Union[str, None] = Header()  
):  
    print(f"user_agent : {user_agent}")  
    return {  
        "message": "Hello users!",  
        "data": {"user_agent": user_agent}  
    }

1

二、自动转换

HeaderPath, QueryCookie 提供的功能之上有一点额外的功能,通常在Python代码中那样使用 user_agent ,而不需要将首字母大写为 User_Agent 或类似的东西,如果出于某些原因,你需要禁用下划线到连字符的自动转换,设置Header的参数 convert_underscoresFalse:

from typing import Union
from fastapi import FastAPI, Header
from typing_extensions import Annotated

app = FastAPI()


@app.get("/users/")
async def get_users(
    strange_header: Annotated[
        Union[str, None], Header(convert_underscores=False)
    ] = None
):
    return {"strange_header": strange_header}

三、重复的 headers

有可能收到重复的headers。相同的header具有多个值,可以在类型声明中使用一个list来定义这些情况。通过一个Python list 的形式获得重复header的所有值,为了声明一个 X-Token header 可以出现多次,代码如下:

from typing import Union, List
from fastapi import FastAPI, Header
from typing_extensions import Annotated

app = FastAPI()


@app.get("/users/")
async def get_users(x_token: Annotated[Union[List[str], None], Header()] = None):
    return {"X-Token values": x_token}

如果http请求发送2个headers,像下面:

X-Token: Teacher Li 
X-Token: Teacher Q

则收到的结果如下:

{ "X-Token values": [ "Teacher Li ", "Teacher Q" ] }

总结

Header 来声明 header , 使用和 Query, PathCookie 相同的模式,不用担心变量中的下划线,FastAPI 会负责转换它们。







lzq599220

FASTAPI系列12-Cookie值

FASTAPI系列 13-header参数

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坦笑&&life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值