【问题标题】:python requests post method results inconsistentpython请求post方法结果不一致
【发布时间】: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


【解决方案1】:

基本上:@Ray 和 @mohammed 建议的问题是请求。 REST 服务是在 ASP.NET Web API 2 平台上编写的,REST 控制器上的模型绑定器期望请求对象 (JSON) 与预期作为输入的模型具有完全相同的结构和拼写。

问题已确定:Python 请求中的问题是 DepositDuration 对象的属性 YearsMonthsDays 前面有 _ 并且没有绑定和默认为 api 上的零,该 api 以适当的响应正确响应。

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

解决方案:我已经从 python 类 DepositDuration 的属性 yearsmonthsdays 中删除了 _,瞧一切正常!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2017-03-07
    相关资源
    最近更新 更多