FastAPI 入门
Matt Eckerle
Software and Data Leader
@app.delete("/items")
def delete_item(item: Item):
if item.id not in item_ids:
# 返回错误
else:
crud.delete_item(item)
return {}
@app.delete("/items")
def delete_item(item: Item):
try:
crud.delete_item(item)
except Exception:
# 返回错误
return {}
100 - 5991 -5)100 - 199)200 - 299)300 - 399)400 - 499)500 - 599)200 - 299)200 OK201 Created202 Accepted204 No Content301 Moved Permantently400 Bad Request404 Not Found500 Internal Server Errorfrom fastapi import FastAPI, HTTPException
app = FastAPI()
@app.delete("/items")
def delete_item(item: Item):
if item.id not in item_ids:
# 发送状态码 404 和具体错误信息的响应
raise HTTPException(status_code=404, detail="Item not found.")
else:
delete_item_in_database(item)
return {}
FastAPI 入门