postman中生成的基于requests库的接口请求代码:
import requests url = "http://**.***.**.**/***-api/****rders/buildOrder" querystring = {"loginId":"e8e6739b087a48eb91345d6ef39180a4"} payload = "{ "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": "软件新城3", "sendMobile": "18191218230", "sendName": "党青亚", "sendPhone": "02912345678", "receiveName": "收件", "receiveMobile": "13300001111", "receivePhone": "", "receiveAddress": "详细", "checkedSender": false, "checkedReciver": false, "customLogisticNo": null, "wrapUpNum": 1, "newSource": "YTO-STEWARD", "status": 0, "stockSaveRequests": { "stockGoodsRequests": [] } }" 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中完全不一样,加了很多的 ,看着很乱
其实只需要在请求时使用: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())