【发布时间】:2017-05-12 11:30:43
【问题描述】:
问题:
使用pythonrequests时的响应和chrome的postman对于同一个请求不一致。
注意事项:
1)网址在https上。
2) json 是请求和响应的数据格式
3)python版本3.x
4) requests 2.2 版
python 代码:
headers = {'Content-Type': 'application/json'}
response = requests.post(self.__url, data=jsonpickle.encode(apiContractObject), headers=headers, verify=True)
print(response.text)
python代码中的响应
{
"productId": 0,
"productName": "testingPy",
"yearsArray": [
{
"key": 0,
"value": {
"outgo": 100.0,
"benefit": 0.0,
"net": -100.0,
"year": "2017-2018",
"age": 0
}
}
]
}
在邮递员中响应相同的请求
{
"productId": 0,
"productName": "testingPy",
"yearsArray": [
{
"key": 0,
"value": {
"outgo": 100,
"benefit": 0,
"net": -100,
"year": "2017-2018",
"age": 0
}
},
{
"key": 1,
"value": {
"outgo": 0,
"benefit": 110.39,
"net": 110.39,
"year": "2018-2019",
"age": 1
}
}
]
}
区别
yearsArray 在 python 代码的响应中有 1 个元素,但 实际上应该有 2 个,如邮递员响应中所见
我是 Python 新手!
编辑:
apiContractObject 就是这个 python 类:
class FDCashflowContract:
"""Contract object for API"""
def __init__(self):
self.projectionType = 0
self.isCumulative = 0
self.dateOfDeposit = ''
self.depositHolderDob = ''
self.depositDuration = DepositDuration(0, 0, 0)
self.depositAmount = 0
self.compoundingFrequency = 0
self.interestPayOutFrequency = 0
self.interestRate = 0,
self.doYouKnowMaturityAmount = True
self.maturityAmount = 0
self.doYouKnowInterestPayOutAmount = True
self.interestPayOutAmount = 0
self.firstInterestPayOutDate = ''
self.productName = 'testingPy'
class DepositDuration:
"""A class that represents the duration object for use as a contract object"""
def __init__(self, years, months, days):
self._years = years
self._months = months
self._days = days
但是传递的实例是这样的:
duration = finfloApiModel.DepositDuration(1, 0, 0)
contract = finfloApiModel.FDCashflowContract()
contract.depositAmount = 100
contract.depositDuration = duration
contract.interestRate = 10
contract.dateOfDeposit = '05-12-2017'
contract.depositHolderDob = '04-27-2017'
contract.isCumulative = 1
contract.projectionType = 1
这是邮递员的请求:
{
"productId": 0,
"projectionType": 1,
"productName": "testingPy",
"isCumulative": 1,
"dateOfDeposit": "2017-05-12T06:26:45.239Z",
"depositHolderDob": "2017-05-12T06:26:45.239Z",
"depositDuration": {
"years": 1,
"months": 0,
"days": 0
},
"depositAmount": 100,
"compoundingFrequency": 0,
"interestPayOutFrequency": 0,
"interestRate": 10,
"doYouKnowMaturityAmount": false,
"maturityAmount": 0,
"doYouKnowInterestPayOutAmount": false,
"interestPayOutAmount": 0
}
【问题讨论】:
-
你传递了什么参数?
-
答案几乎肯定在于您传递给请求的参数。你也可以发一下吗?
-
@mohammad 参数信息添加到问题中
-
@Ray参数信息添加到问题中
-
我看到 depositHolderDob 在 python 和 postman 中有不同的值..
标签: json python-3.x python-requests postman