You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FastAPI中Header参数在Swagger文档中显示为Query参数的问题排查求助

FastAPI中Header参数在Swagger文档中显示为Query参数的问题排查求助

我照着FastAPI官方文档里的请求头参数示例写了一个接口,本来是要读取请求里的User Agent请求头,但运行后发现自动生成的Swagger文档里,这个参数居然被显示成了Query参数,完全不符合预期。有没有大佬能帮我排查下哪里配置错了?

我的代码如下:

from typing import Annotated

from fastapi import FastAPI, Header

app = FastAPI()

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

我当前使用的Python版本是3.10。

备注:内容来源于stack exchange,提问作者poiuytrez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 10:39:50