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())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM