python Django:字节索引必须是整数或切片,而不是字符串错误

y1aodyip  于 2023-02-07  发布在  Python
关注(0)|答案(2)|浏览(104)

我正尝试使用webhook从有效负载获取一些数据,在我发送的响应中,我添加了应随有效负载返回的 meta数据,这是我的操作方式(* 以下代码 *),如果您仔细查看键,这里有一个名为meta的键,现在我正尝试从我的webhook视图获取元数据中的一些数据,如下所示:

查看次数.py网页挂钩

@csrf_exempt
@require_http_methods(['POST', 'GET'])
def webhook(request):
    payload = request.body
    order_id = payload['meta']['order_id']
    print(order_id)
    return HttpResponse("testing...")

向支付网关发送响应

@csrf_exempt
def process_payment(request, name, email, amount, order_id):
    auth_token = "FLWSECK_TEST-9efb9ee17d8afe40c6c890294a1163de-X"
    hed = {'Authorization': 'Bearer ' + auth_token}
    data = {
            "tx_ref":''+str(math.floor(1000000 + random.random()*9000000)),
            "amount":amount,
            "currency":"USD",
            "order_id":order_id,
            "redirect_url":f"http://localhost:3000/service-detail/booking/confirmation/{order_id}",
            "payment_options":"card",
            "meta":{
                "order_id":order_id,
                "consumer_id":23,
                "consumer_mac":"92a3-912ba-1192a"
            },
            "customer":{
                "email":email,
                "name":name,
                "order_id":order_id
            },
            "customizations":{
                "title":"ForecastFaceoff",
                "description":"Leading Political Betting Platform",
                "logo":"https://i.im.ge/2022/08/03m/FELzix.stridearn-high-quality-logo-circle.jpg"
                }
            }
    url = ' https://api.flutterwave.com/v3/payments'
    response = requests.post(url, json=data, headers=hed)
    response=response.json()
    link=response['data']['link']
    return redirect(link)

有效负载

{
  "event": "charge.completed",
  "data": {
    "id": 4136234873,
    "tx_ref": "6473247093",
    "flw_ref": "FLW-MOCK-b33e86ab2342316fec664110e8eb842a3c2f956",
    "device_fingerprint": "df38c8854324598c54e16feacc65348a5e446",
    "amount": 152,
    "currency": "USD",
    "charged_amount": 152,
    "app_fee": 5.78,
    "merchant_fee": 0,
    "processor_response": "Approved. Successful",
    "auth_model": "VBVSECUREertrCODE",
    "ip": "52.209.154.143",
    "narration": "CARD Transaction ",
    "status": "successful",
    "payment_type": "card",
    "created_at": "2023-02-06T11:19:45.000Z",
    "account_id": 685622,
    "customer": {
      "id": 1970338,
      "name": "destiny ",
      "phone_number": null,
      "email": "******@gmail.com",
      "created_at": "2023-02-06T11:19:45.000Z"
    },
    "card": {
      "first_6digits": "653188",
      "last_4digits": "2340",
      "issuer": "MASTERCARD  CREDIT",
      "country": "NG",
      "type": "MASTERCARD",
      "expiry": "49/32"
    }
  },
  "event.type": "CARD_TRANSACTION"
}

元数据甚至没有显示在有效负载中,在我可以获取与它一起发送的数据之前,它必须显示在有效负载中吗?

gmxoilav

gmxoilav1#

body是字节而不是字符串,因此需要将字符串作为JSON加载。

payload = json.loads(request.body)
ijnw1ujt

ijnw1ujt2#

元数据不在payload字典中。首先尝试从负载中提取数据密钥,如下所示:

@csrf_exempt
@require_http_methods(['POST', 'GET'])
def webhook(request):
    payload = json.loads(request.body.decode("utf-8"))
    data = payload.get("data")
    if data:
        order_id = data.get("meta", {}).get("order_id")
        print(order_id)
    return HttpResponse("just testing")

编辑:

上面注解JSONDecodeError: Expecting value: line 1 column 1 (char 0) indicates that json.loads()中描述的错误消息要求输入JSON字符串。
请求正文可能为空。您可以通过在调用json.loads()之前打印request.body来检查。

相关问题