The status_code parameter receives a number with the HTTP status code.Info status_code can alternatively also receive an IntEnum, such as Python's http.HTTPStatus.It will:Return that status code in the response.
status_code 参数接收表示 HTTP 状态码的数字。说明 status_code 还能接收 IntEnum 类型,比如 Python 的 http.HTTPStatus。它可以:在响应中返回状态码 在OpenAPI 概图(及用户界面)中存档:笔记 某些响应状态码表示响应没有响应体(参阅下一章)。 FastAPI 可以进行识别,并生成表明无响应体的 OpenAPI 文档。
app = FastAPI()@app.post("/items/", status_code=status.HTTP_201_CREATED)asyncdefcreate_item(name:str):return{"name": name} 更推荐用这个,因为变量名会包含状态码+含义 fastapi.status是直接来自starlette.status,提供的东西都是一样的 HTTPStatus 的栗子 fromhttpimportHTTPStatus app = FastAPI()@app...
from fastapiimportstatus app=FastAPI()@app.post("/items/",status_code=status.HTTP_201_CREATED)asyncdefcreate_item(name:str):return{"name":name} 更推荐用这个,因为变量名会包含状态码+含义 是直接来自 starlette.status ,提供的东西都是一样的 fastapi.status HTTPStatus 的栗子 代码语言:javascript 代码...
response.historty 获取跳转前的URL response.json 获取json数据 常用的就前3个,text返回文本string,content返回文本bytes,status_code返回状态码,状态码分类和含义如下: 状态码分类含义 1** 信息 服务器收到请求,需要请求者继续执行操作 2** 成功 请求被成功接收并处理 3** 重定向 需要进一步的操作以完成请求 4...
from fastapi import statusapp = FastAPI()@app.post("/items/", status_code=status.HTTP_201_CREATED)async def create_item(name: str):return {"name": name} 更推荐用这个,因为变量名会包含状态码+含义 fastapi.status是直接来自starlette.status,提供的东西都是一样的 ...
@app.post("/items/", status_code=status.HTTP_201_CREATED) async def create_item(name: str): return {"name": name} 1. 2. 3. 4. 5. 6. 7. 更推荐用这个,因为变量名会包含状态码+含义 fastapi.status 是直接来自 starlette.status ,提供的东西都是一样的 ...
get('/hello', tags=['示例一']) def hello(): return {'hello': 'FastAPI'} @app.get('/', response_class=HTMLResponse) async def index(request: Request): return templates.TemplateResponse('index.html', {'request': request}) 视图函数 index 除了是一个协程函数之外,与 hello 最大的不同...
app=FastAPI()@app.post("/items/",status_code=201)asyncdefcreate_item(name:str):return{"name":name} status_code也可以是IntEnum,比如Python的http.HTTPStatus。 常见响应状态码: 100以上,信息;很少直接使用; 200以上,成功;200是OK,201是Created,204是No Content; ...
Read more about it in the FastAPI docs about Response Status Code.Example¶from fastapi import FastAPI, status app = FastAPI() @app.get("/items/", status_code=status.HTTP_418_IM_A_TEAPOT) def read_items(): return [{"name": "Plumbus"}, {"name": "Portal Gun"}] ...