2.fastapi基础

2.fastapi基础

  • hellow word

    from typing import Optional
    
    from fastapi import FastAPI
    from pydantic import BaseModel
    
    app = FastAPI()
    
    
    class CityInfo(BaseModel):
        province: str
        country: str
        is_affected: Optional[bool] = None  # 与bool的区别是可以不传,默认是null
    
    
    # @app.get('/')
    # def hello_world():
    #     return {' hello': 'world '}
    #
    #
    # @app.get('/city/{city}')   #city 是路径参数(必填参数),quer_string是查询参数(可选填)
    # def result(city: str, query_string: Optional[str] = None):
    #     return {'city': city, 'query_string': query_string}
    #
    # # 启动命令:uvicorn helloworld : app --reload  # reload 模式
    #
    # @app.put( '/city/{city}' )
    # def result( city : str, city_info: CityInfo ) :
    #     return {'city': city,'country': city_info.country,'is_affected': city_info.is_affected}
    
    #################################################异步模式########################################
    @app.get('/')
    async def hello_world():
        return {' hello': 'world '}
    
    
    @app.get('/city/{city}')   #city 是路径参数(必填参数),quer_string是查询参数(可选填)
    async def result(city: str, query_string: Optional[str] = None):
        return {'city': city, 'query_string': query_string}
    
    # 启动命令:uvicorn helloworld : app --reload
    
    @app.put( '/city/{city}' )
    async def result( city : str, city_info: CityInfo ) :  # 也可以是自定义的类型City_info
        return {'city': city,'country': city_info.country,'is_affected': city_info.is_affected}
    
  • 自带接口说明文档docs和redoc

    • docs 可交互

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Dfb5ypD2-1636981667885)(2.fastapi%E5%9F%BA%E7%A1%80.assets/image-20210606170145367.png)]

    • redoc

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EE9Fy02g-1636981667888)(2.fastapi%E5%9F%BA%E7%A1%80.assets/image-20210606170221323.png)]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小帆芽芽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值