1. 程式人生 > 其它 >fastapi中post請求巢狀資料

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

作者:做夢的人(小姐姐)
出處:https://www.cnblogs.com/chongyou/
本文版權歸作者,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線。
如果文中有什麼錯誤,歡迎指出。以免更多的人被誤導。
微訊號:18582559217