python:关于postman中生成的基于requests库的接口代码

postman中生成的基于requests库的接口请求代码:

import requests

url = "http://**.***.**.**/***-api/****rders/buildOrder"

querystring = {"loginId":"e8e6739b087a48eb91345d6ef39180a4"}

payload = "{\n    \"customCode\": \"\",\n    \"apvalue\": 0,\n    \"agentValue\": 0,\n    \"supportValue\": 0,\n    \"district\": [],\n    \"districtSend\": [],\n    \"orderProducts\": [\n        {\n            \"id\": \"\",\n            \"itemCount\": 1,\n            \"itemName\": \"2\",\n            \"itemValue\": \"\",\n            \"itemWeight\": 0,\n            \"remark\": \"\"\n        }\n    ],\n    \"fromCityCode\": \"DIST_INTERNAL_610100\",\n    \"fromCityName\": \"西安市\",\n    \"fromDistrictCode\": \"DIST_INTERNAL_610113\",\n    \"fromDistrictName\": \"雁塔区\",\n    \"fromProvinceCode\": \"DIST_INTERNAL_610000\",\n    \"fromProvinceName\": \"陕西省\",\n    \"toCityCode\": \"DIST_INTERNAL_150100\",\n    \"toCityName\": \"呼和浩特市\",\n    \"toDistrictCode\": \"DIST_INTERNAL_150102\",\n    \"toDistrictName\": \"新城区\",\n    \"toProvinceCode\": \"DIST_INTERNAL_150000\",\n    \"toProvinceName\": \"内蒙古自治区\",\n    \"sendAddress\": \"软件新城3\",\n    \"sendMobile\": \"18191218230\",\n    \"sendName\": \"党青亚\",\n    \"sendPhone\": \"02912345678\",\n    \"receiveName\": \"收件\",\n    \"receiveMobile\": \"13300001111\",\n    \"receivePhone\": \"\",\n    \"receiveAddress\": \"详细\",\n    \"checkedSender\": false,\n    \"checkedReciver\": false,\n    \"customLogisticNo\": null,\n    \"wrapUpNum\": 1,\n    \"newSource\": \"YTO-STEWARD\",\n    \"status\": 0,\n    \"stockSaveRequests\": {\n        \"stockGoodsRequests\": []\n    }\n}"
headers = {
    'Content-Type': "application/json",
    'jwt-token': "eyJhbGciOiJIUzI1NiJ9.eyJleHAi***xNzEsInN1YzMSoqKjM4MTM3XCIsXCJnpcmVkXCI6ZmFsc2UsXCJhY2NvdW50Tm9uTG9ja2VkXCI6ZmFsc2V9In0.t6S6_54BBSZvthndhQxBVey7FuYEQ4oC9ncmbodzasY",
    'cache-control': "no-cache",
    'Postman-Token': "447abf40-88a9-4cb5-bdec-9aa388f6f86e"
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

可以看到本来入参是一个json,但生成是变味了一个字符串,和postman中完全不一样,加了很多的\n,看着很乱

 

其实只需要在请求时使用:requests.post(url, json=data, headers=headers)就可以了,入参就可以直接写为字典了,将入参赋值给json参数

import requests

url = "http://**.*.*.*/**-api/**Orders/buildOrder?loginId=e8e6739b087a48eb91345d6ef39180a4"

data = {
    "customCode": "",
    "apvalue": 0,
    "agentValue": 0,
    "supportValue": 0,
    "district": [],
    "districtSend": [],
    "orderProducts": [
        {
            "id": "",
            "itemCount": 1,
            "itemName": "2",
            "itemValue": "",
            "itemWeight": 0,
            "remark": ""
        }
    ],
    "fromCityCode": "DIST_INTERNAL_610100",
    "fromCityName": "**",
    "fromDistrictCode": "DIST_INTERNAL_610113",
    "fromDistrictName": "**",
    "fromProvinceCode": "DIST_INTERNAL_610000",
    "fromProvinceName": "**",
    "toCityCode": "DIST_INTERNAL_150100",
    "toCityName": "呼和浩特市",
    "toDistrictCode": "DIST_INTERNAL_150102",
    "toDistrictName": "新城区",
    "toProvinceCode": "DIST_INTERNAL_150000",
    "toProvinceName": "内蒙古自治区",
    "sendAddress": "**",
    "sendMobile": "***",
    "sendName": "**",
    "sendPhone": "02912345678",
    "receiveName": "收件",
    "receiveMobile": "13300001111",
    "receivePhone": "",
    "receiveAddress": "详细",
    "checkedSender": False,
    "checkedReciver": False,
    "customLogisticNo": "",
    "wrapUpNum": 1,
    "newSource": "YTO-STEWARD",
    "status": 0,
    "stockSaveRequests": {
        "stockGoodsRequests": []
    }
}

headers = {"Content-Type": "application/json", "jwt-token": "eyJhCI6ZmFsc2V9In0.t6S6_54BBSZvthndhQxBVey7FuYEQ4oC9ncmbodzasY"}
res = requests.post(url, json=data, headers=headers)
print(res.json())

 

posted @ 2020-05-21 15:39  秋寻草  阅读(1058)  评论(0编辑  收藏  举报