2017-05-12 42 views
1

問題: 使用python requests和chrome的郵遞員對同一請求不一致時的響應。python請求post方法結果不一致

需要注意的事項: 1)網址在https上。 2)JSON是用於請求和響應 3)蟒版本3中的數據格式 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是這條巨蟒類:

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 
} 
+1

你傳遞了​​什麼參數? – mohammad

+1

答案几乎肯定存在於傳遞給請求的參數中。你可以發佈這個呢? – Ray

+0

@mohammad參數信息添加到問題 – amythn04

回答

0

基本上:作爲@Ray & @mohammed建議的問題是請求。 REST服務是在ASP.NET Web API 2平臺上編寫的,REST控制器上的模型綁定器期望請求對象(JSON)與預期作爲輸入的模型具有完全相同的結構和拼寫。

問題識別:在Python請求的問題是,所述DepositDuration對象的屬性YearsMonthsDays均前面有_和沒有約束力,缺省值爲零其上正確地與一個適當的響應響應的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