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

GET请求处理声明未设置header名称,文档中响应header名称为空如何解决?

FastAPI响应Header名称显示为空的问题解决
  • 问题根源:你在responses配置里的headers使用了数组列表格式,但FastAPI要求这里必须是字典格式——键为Header名称,值为对应的描述对象。这种格式错误导致文档解析器无法识别Header名称,同时触发“GET请求处理声明中未设置header名称"headers"”的提示。

  • 修正后的代码示例:

@router.get('/hash_header', response_model=models.hash_out, tags=["use-hash"], responses={
        200: {
            "model": models.hash_out,
            "description": "Return hash code",
            "headers": {
                "Secret-Code": {
                    "description": "Secret code",
                    "type": "string"
                }
            }
        }        
})
  • 说明:将原代码中包裹Header的数组[]替换为字典{},直接把Secret-Code作为键,对应的描述、类型作为值,这样生成的API文档就能正确显示Header名称,同时解决错误提示问题。

内容的提问来源于stack exchange,提问作者Pavel Gribov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 04:26:02