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)

 

 

 

 

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐