一、手动填写返回状态码
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}View Code
接口1:/status_code
返回值:{ "status_code": 200 }
二、使用fastapi中的status类
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}View Code
接口2:/status_attributes
返回值:{ "status_code": 200 }