• python+uvicorn+fastapi (二)


    路径参数

    路径参数 item_id 的值将作为参数 item_id 传递给你的函数。

    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get("/case/{cid}")
    def read_case(cid):
        return {"id": cid}
    

    如果你运行示例并访问 http://127.0.0.1:8002/case/foo,将会看到如下响应:

    {"id":"foo"}
    

    数据转换

    如果你访问 http://127.0.0.1:8002/case/1,将会看到如下响应

    {"id":1}
    

    有类型的路径参数

    @app.get("/items/{item_id}")
    def read_item1(item_id: int, q: Optional[str] = None):
        return {"item_id": item_id, "q": q}
    

    如果你访问http://127.0.0.1:8002/items/100?q=1,将会看到如下响应

    {"item_id":100,"q":"1"}
    

    数据校验

    如果访问http://127.0.0.1:8002/items/foo?q=1,会看到如下响应

    {
    	"detail": [
    		{
    			"loc": [
    				"path",
    				"item_id"
    			],
    			"msg": "value is not a valid integer",
    			"type": "type_error.integer"
    		}
    	]
    }
    

    因为路径参数 item_id 传入的值为 "foo",它不是一个 int

    Pydantic

    所有的数据校验都由 Pydantic 在幕后完成

    查询参数

    声明不属于路径参数的其他函数参数时,它们将被自动解释为"查询字符串"参数

    from fastapi import FastAPI
    
    app = FastAPI()
    
    fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
    
    
    @app.get("/items/")
    def read_item(skip: int = 0, limit: int = 10):
        return fake_items_db[skip : skip + limit]
    

    查询字符串是键值对的集合,这些键值对位于 URL 的 之后,并以 & 符号分隔。

    例如,在以下 url 中:

    http://127.0.0.1:8000/items/?skip=0&limit=10
    

    ...查询参数为:

    • skip:对应的值为 0,这里是代码指定的默认值
    • limit:对应的值为 10,这里是代码指定的默认值

    请求体

    你的 API 几乎总是要发送响应体。但是客户端并不总是需要发送请求体。

    我们使用 Pydantic 模型来声明请求体,并能够获得它们所具有的所有能力和优点。

    这里要注意一点:

    你不能使用 GET 操作(HTTP 方法)发送请求体。

    要发送数据,你必须使用下列方法之一:POST(较常见)、PUTDELETEPATCH

    DEMO

    数据模型继承自pydantic的BaseModel,Pydantic 会帮你处理模型内定义的字段的数据校验的问题。

    from typing import Optional
    
    from fastapi import FastAPI
    from pydantic import BaseModel
    
    
    class Item(BaseModel):
        name: str
        description: Optional[str] = None
        price: float
        tax: Optional[float] = None
    
    
    app = FastAPI()
    
    @app.get("/items/{item_id}")
    def read_item1(item_id: int, q: Optional[str] = None):
        return {"item_id": item_id, "q": q}
    
    @app.post("/items/")
    def create_item(item: Item):
        return item
    

    示例代码包含一个get请求一个post请求,在pycharm上看到数据模型Item的内容会有红色波浪线,不要紧,不会影响程序运行。

    我们访问http://127.0.0.1:8002/items/1?q=123,可以看到get请求的响应如下

    {"item_id":1,"q":"123"}
    

    然后再发送一个post请求看下响应,post请求的url和请求体可以这样写

    url

    http://127.0.0.1:8002/items/

    body

    {   
        "name": "kpc测试商品",
        "description": "我是伟大的post接口描述",
        "price": 100.00,
        "tax": "0.13"
    }
    

    响应的内容如下

    {
        "name": "kpc测试商品",
        "description": "我是伟大的post接口描述",
        "price": 100.0,
        "tax": 0.13
    }
    
    更多学习笔记移步 https://www.cnblogs.com/kknote
  • 相关阅读:
    SQL语句中日期相减注意事项
    WinRAR打压缩的几个命令
    SQL中使用WITH AS提高性能-使用公用表表达式(CTE)简化嵌套SQL
    某一列选最大值 其他内容不同要求随机选择的情况下去除重复行的方法
    Datastage里Aggregator的一些注意事项
    几个网络流行词的翻译
    找工作的网站
    WPF版连连看,swing版俄罗斯方块源码
    mirrortankWar
    Delphi著名皮肤控件库横向对比。
  • 原文地址:https://www.cnblogs.com/kknote/p/15000198.html
Copyright © 2020-2023  润新知