fastAPI(6)--Response自定义状态码
status_code参数接收带有HTTP状态代码的数字可以从fastapi.status导入状态码常量,便于使用和记忆#!/usr/bin/env python# encoding: utf-8from fastapi import FastAPI, statusimport uvicornapp = FastAPI()#status_code参数接收带有HTTP状态代码的数字@app.post
·
status_code参数接收带有HTTP状态代码的数字
可以从 fastapi.status
导入状态码常量,便于使用和记忆
#!/usr/bin/env python
# encoding: utf-8
from fastapi import FastAPI, status
import uvicorn
app = FastAPI()
#status_code参数接收带有HTTP状态代码的数字
@app.post('/items/', status_code=201)
async def create_item(name: str):
return {"name": name}
@app.get('/items2/', status_code=201)
async def create_item2(name: str):
return {"name": name}
@app.post('/items3/', status_code=status.HTTP_404_NOT_FOUND)
async def create_item3(name: str):
print('HTTP 404', status.HTTP_404_NOT_FOUND)
return {"name": name}
if __name__ == '__main__':
uvicorn.run(app=app, host='127.0.0.1', port=8000)
更多推荐
已为社区贡献5条内容
所有评论(0)