【问题标题】:Safaricom Daraja: Duplicate json key detectedSafaricom Daraja:检测到重复的 json 密钥
【发布时间】:2022-10-30 07:10:49
【问题描述】:

我正在通过 Safaricom 在线实施 Lipa na M-Pesa。但是,当我提出请求时,我收到一条错误消息,提示检测到重复的 json 键。

有效载荷:

payload = {
    "BusinessShortCode": 174379, 
    "Password": "the password", 
    "Timestamp": "20220817050503", 
    "TransactionType": "CustomerPayBillOnline", 
    "Amount": 5, 
    "PartyA": 254714895622, 
    "PartyB": 174379, 
    "PhoneNumber": 254714895622, 
    "CallBackURL": "https://58fa-102-68-78-50.in.ngrok.io/api/v1/payments/callback/", 
    "AccountReference": "Payment", 
    "TransactionDesc": "Payment for K"
}

标题:

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer access_token",
}

请求:

response = requests.request("POST", 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest', headers=headers, data=payload)

我得到的回应:

{
   'responseId': '82699-21939686', 
   'responseCode': '400', 
   'responseDesc': 'Duplicate json key detected'
}

此错误的原因可能是什么,我该如何解决?

【问题讨论】:

    标签: json python-3.x django django-rest-framework


    【解决方案1】:

    问题出在标头中,请求默认通过Content-Type, 摆脱它可以解决问题。

    【讨论】:

      【解决方案2】:

      尽管在 Safaricom 的 M-pesa Daraja 代码示例中提供了它,但请尝试更改此部分以使其成为;将 body=payload 重命名为 json=payload

      response = requests.request("POST",your-url, headers=headers, json=payload)
      

      代替

      response = requests.request("POST", url , headers=headers, data=payload) 
      

      按照建议从标题中删除 Content-Type 给了我另一个错误;

      {'requestId': '12345-12345-1',
      'errorCode': '400.002.02',
      'errorMessage': 'Bad Request - Invalid BusinessShortCode'}
      

      为我工作希望它对你有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-19
        • 2021-09-16
        • 2016-10-22
        • 1970-01-01
        • 1970-01-01
        • 2011-10-07
        • 1970-01-01
        • 2017-01-29
        相关资源
        最近更新 更多