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
相关产品推荐
相关产品推荐

