• fastapi中post请求嵌套数据 做梦的人


    背景:最近要写一个服务器的测试桩,post请求 给服务器,服务器在把请求的数据解析出来

    post的请求格式为:

    {
        "total": 1,
        "rows": [
            {
                "appkey": "abc",
                "msgid": 123,
                "regid": "xxxxxx",
                "channel": "app",
                "sub_channel": 0,
                "platform": "android",
                "itime": 57888847848,
                "status": 0,
                "err_code": 0,
                "reason": "",
                "loss_source": 1,
                "data_msgtype": 1
            }
        ]
    }
    

      解包实现为:

    # -*- coding:utf-8 -*-
    #@Time : 2022/2/15 14:22
    #@Author: 张君
    #@File : PrivateCloud.py
    
    import uvicorn
    
    from fastapi import FastAPI
    from pydantic import BaseModel
    from typing import List, Set
    
    app = FastAPI()
    
    class Item(BaseModel):
    
        appkey: str
        msgid: int
        regid: str
        channel: str
        sub_channel:int
        platform: str
        itime: str
        status: int
        err_code: int
        reason: str
        loss_source: int
        data_msgtype: int
    
    class Offer(BaseModel):
        total: int
        rows: List[Item]
    
    @app.post("/offers/")
    async def create_offer(*, offer: Offer):
        return offer
    
    if __name__ == '__main__':
       uvicorn.run(app='main:app', host="127.0.0.1", port=8200, reload=True, debug=True)

    在服务器中部署请求日志

      

    参考文章如下:

    https://www.cnblogs.com/xiao987334176/p/13099223.html

  • 相关阅读:
    2-3-4 tree留坑
    CCPC final Cockroaches
    对拍模板
    使用cronolog按日期分割日志
    linux git 命了
    变量加减乘除运算
    根据pom标签修改
    根据符号获取字符
    shell循环字符串数组
    git ssh key配置
  • 原文地址:https://www.cnblogs.com/chongyou/p/15897933.html
Copyright © 2020-2023  润新知