FastAPI学习笔记(一)-12.响应模型中的响应状态码
一、手动填写返回状态码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ''' 2 @author:invoker 3 @project:fastapi202108 4 @file: chapter042.py 5 @contact:invoker2021@126.com 6 @descript: 7 @Date:2021/8/6 16:58 8 @version: Python 3.7.8 9 ''' 10 from fastapi import APIRouter,status 11 12 app042 = APIRouter() 13 14 """ 15 响应状态码 16 """ 17 18 # 接口1:放回状态码手动填写 19 @app042.post('/status_code',status_code=200) 20 async def status_code(): 21 return{"status_code":200}
接口1:/status_code
返回值:{ "status_code": 200 }
二、使用fastapi中的status类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ''' 2 @author:invoker 3 @project:fastapi202108 4 @file: chapter042.py 5 @contact:invoker2021@126.com 6 @descript: 7 @Date:2021/8/6 16:58 8 @version: Python 3.7.8 9 ''' 10 from fastapi import APIRouter,status 11 12 app042 = APIRouter() 13 14 """ 15 响应状态码 16 """ 17 18 # 接口1:放回状态码手动填写 19 @app042.post('/status_code',status_code=200) 20 async def status_code(): 21 return{"status_code":200} 22 23 # 接口2:使用status类 24 @app042.post('/status_attributes',status_code=status.HTTP_200_OK) 25 async def status_attributes(): 26 return{"status_code":status.HTTP_200_OK}
接口2:/status_attributes
返回值:{ "status_code": 200 }
本文来自博客园,作者:kaer_invoker,转载请注明原文链接:https://www.cnblogs.com/invoker2021/p/15109798.html