Read more about it in theFastAPI docs about Response Status Code. Example¶ fromfastapiimportFastAPI,statusapp=FastAPI()@app.get("/items/",status_code=status.HTTP_418_IM_A_TEAPOT)defread_items():return[{"name":"Plumbus"},{"name":"Portal Gun"}] ...
from fastapi import FastAPI, status app = FastAPI() @app.post("/items/", status_code=201) async def create_item(name: str): return {"name": name} status_code 参数属于装饰器中的参数,而非 路径操作函数 的参数。它接收一个表示 HTTP 状态码的数字,或支持 IntEnum 类型,例如 ...
一、 FASTAPI系列 15-响应状态码status_code 前言 与指定响应模型的方式相同,你也可以在以下任意的_路径操作_中使用status_code 参数来声明用于响应的HTTP 状态码: @app.get() @app.post() @app.put() @app.delete() 一、响应状态码 from fastapi import FastAPI app = FastAPI() @app.post(...
app = FastAPI()@app.post("/items/", status_code=201)asyncdefcreate_item(name:str):return{"name": name} 注意,status_code是「装饰器」方法(get,post等)的一个参数。不像之前的所有参数和请求体,它不属于_路径操作函数_。 status_code参数接收一个表示 HTTP 状态码的数字。 status_code也能够接收一...
FastAPI Response Status Code fastapi/fastapi 0.115.12 84k 7.3k FastAPI Learn Tutorial - User Guide Response Status Code¶ The same way you can specify a response model, you can also declare the HTTP status code used for the response with the parameterstatus_codein any of thepath operations...
Handling HTTP Status Codes in FastAPI FastAPI provides a simple and intuitive way to return specific HTTP status codes in your API endpoints. You can use thestatus_codeparameter in theResponseclass to set the desired status code for a response. ...
app=FastAPI()@app.post("/items/",status_code=HTTPStatus.CREATED)asyncdefcreate_item(name:str):return{"name":name} status_code 的作用 在响应中返回该状态代码 在OpenAPI Schema 中记录它,也会显示在 SwaggerAPI文档中 正确传参的请求结果 查看Swagger API 文档 ...
from fastapi import FastAPI app = FastAPI() @app.post("/items/", status_code=201) async def create_item(name: str): return {"name": name} 1. 2. 3. 4. 5. 6. 7. 8. 注意,status_code是「装饰器」方法(get,post等)的一个参数。不像之前的所有参数和请求体,它不属于_路径操作函数_。
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,提供的东西都是一样的 ...
数据库,路由蓝图,数据验证等问题在 FastAPI 中的具体操作和一些自己碰到的坑,分享给正在进攻 FastAPI ...