如何将请求头扩展输出到OpenAPI文档中



在我看来,我们可以为以下字符串的请求头编写

paths:
/ping:
get:
summary: Checks if the server is alive
parameters:
- in: header
name: X-Request-ID
schema:
type: string
format: uuid
required: true

如何在FastAPI的情况下编写类似的代码?

我按照上面提到的链接方式如果它能帮助你,我很高兴

@router.get(
"/code/{code}",
summary="codecheck",
description="code check with token check",
responses={
st.HTTP_200_OK: {
"content": {"application/txt": {
"example": "code check was successful"
}}
},
st.HTTP_400_BAD_REQUEST: {
"content": {"application/json": {
"example": "Request header is empty"
}}
},
st.HTTP_403_FORBIDDEN: {
"content": {"application/json": {
"example": "a-token is unmatched"
}}
},
st.HTTP_500_INTERNAL_SERVER_ERROR: {
"content": {"application/json": {
"example": "Server internal error"
}}
}
})
async def code_check(
code: str, request: Request,
a_token: str|None=Header(default=None)):
ctl = mainctl()
res: Response_data = await ctl.code(code, request)
if res.status_code == st.HTTP_200_OK: return res.text
raise HTTPException(status_code=res.status_code, detail=res.detail)

我刚刚添加了";a_tonk:str|None=Header(默认值=None("response_data是我自己使用pydantic的类,顺便说一下

FastAPI向openapi文档添加标题详细信息

最新更新