Django: byte indices must be integers or slices, not str error

I am trying to get some data from a payload using a webhook, in the response that i sent along side i added the meta data that should return with the payload and this is how i did it (code below), if you take a close look at the keys, there i one called meta, now i am trying to get some of the data that is in the meta from my webhook view like this

views.py webhook

@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...")

Sending the reponse to the payment gateway

@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)

payload

{
  "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"
}

The metadata is not even showing in the payload, must it show up there in the payload before i can grab the data that is sent with it?

UPDATE
I have tried converting my payload to a json like this
payload = json.loads(request.body)

but i got this error

JSONDecodeError at /api/webhook/
Expecting value: line 1 column 1 (char 0)

In general terms, yes. You can’t get any valid results if you’re looking for a key in a dict that doesn’t exist.

Which “payload” are you showing here?
(I’m referring to the block that looks like this:

In other words, what were you looking at to get this?

You have:

What do you see if you add a print(payload) statement after the payload = request.body line?

What do you get if you print(response.text) after you issue the request?

I’ve tried looking through the flutterwave docs to understand what’s going on here, but I’m not finding much useful information.

From what I do see, I don’t see any way for them to pass the meta information back to you in the webhook.

My guess is that you might get that data back in your response object, allowing you to link that meta data with some type of identifier in the response, perhaps the flw_ref. You would then save that meta data and the flw_ref in some model.
When the webhook is called, you would then use the flw_ref from the payload to find the other meta data you need.

I am showing the payload i got back from flutterwave after every successfull or failed payment, by print(payload), that is the dictionary that is been printed on my terminal, i have added the webhook url to flutterwave dashboard which is enabling me to get back the payload.

what i see in my terminal is the dictionary that i have pasted in the question, the payload.

I tried getting the metadata like this because after looking at stripe api documentation on meta-data (Stripe API reference – Metadata – Python) i thought it might actually work, but it doesn’t seem to be the case.

  1. how do i go about getting the data back in my response object?
  2. Is my response object the dictionary that is been printed out in my terminal?

You’re already doing it:

See Quickstart — Requests 2.31.0 documentation

You’re not showing any print statements for a dictionary here so I can’t answer that.

I just tried printing the reponse it seems there is not even a key meta in it

print(response)
################ Terminal Response
{'status': 'success', 'message': 'Hosted Link', 'data': {'link': 'https://ravemodal-dev.herokuapp.com/v3/hosted/pay/c6aadf3678a3c113ac6d'}}

would it still be possible to save any data to a model as you have instructed since there is no over values other than status, message, data{“link”}?

You can save anything to a model that you wish. I’m just not sure that this is going to be of much help.

Maybe you need to use the tx_ref as your key. You obviously need some way to link your request to the data sent to your webhook.

But if the flutterwave docs don’t address the addition of user-supplied metadata in their transactions, I’m not sure there’s much you can do about it.

1 Like