yaml格式为请求内容

时间:2019-01-07 09:26:15

标签: python json request yaml

我想将发布请求正文从json转换为yaml 原始json格式如下:

{
    "traceId": "1000000316028",
    "orderDto": {
        "totalPrice": "0.01",
        "baseList": [],
        "hongbaoSn": "",
        "couponSn": "",
        "shelfCode": "2SvNXIz56rL0EU5PpSpzwA..",
        "additionalPurchaseList": [],
        "bindingList": [{
            "baseSku": {
                "count": 1,
                "skuId": 1001088
            },
            "additionalSku": []
        }]
    }
}

然后将其转换为yaml格式,如下所示:

traceId: 1000000316028
orderDto:
  totalPrice: 0.01
  baseList: []
  hongbaoSn:
  couponSn:
  shelfCode: 2SvNXIz56rL0EU5PpSpzwA..
  additionalPurchaseList: []
  bindingList:
  - baseSku: {count: 1, skuId: 1001088}
    addtionalSku: []

并将其作为发布主体放入python请求中,如下所示:

response = requests.request("POST", url, data=json.dumps(data), headers=headers)

但这是错误的,响应代码为-1。 我是从邮递员那里复制的,正确的正文格式是这样的:

  

data =“ {\ n \ t \” traceId \“:\” 1000000316028 \“,\ n \ t \” orderDto \“:{\ n \ t \ t \” totalPrice \“:\” 0.01 \ “,\ n \ t \ t \” baseList \“:[],\ n \ t \ t \” hongbaoSn \“:\” \“,\ n \ t \ t \” couponSn \“:\” \“ ,\ n \ t \ t \“ shelfCode \”:\“ 2SvNXIz56rL0EU5PpSpzwA .. \”,\ n \ t \ t \“ additionalPurchaseList \”:[],\ n \ t \ t \“ bindingList \”:[{ \ n \ t \ t \ t \“ baseSku \”:{\ n \ t \ t \ t \ t \“ count \”:1,\ n \ t \ t \ t \ t \“ skuId \”:1001088 \ n \ t \ t \ t},\ n \ t \ t \ t \“ additionalSku \”:[] \ n \ t \ t}] \ n \ t} \ n}“

这可以响应代码200。

但是如果我要使用yaml格式,哪里错了?

非常感谢!

1 个答案:

答案 0 :(得分:0)

解决了。 我修改了我的Yaml格式,如下所示:

traceId: '1000000316028'
orderDto:
  totalPrice: '0.01'
  baseList: []
  hongbaoSn: ''
  couponSn: ''
  shelfCode: 2SvNXIz56rL0EU5PpSpzwA..
  additionalPurchaseList: []
  bindingList:
  - baseSku:
      count: 1
      skuId: 1001088
    additionalSku: []