AttributeError: 'bytes' object has no attribute 'amount'

I got back this response from an api call

b'response=1&responseMessage=APPROVED&noticeMessage=New+Card+Stored+-+New+Customer+Created+-+New+Order+Created+-+Order+Marked+as+Paid&date=2023-04-14&time=09%3A00%3A57&type=purchase&amount=100.00&cardHolderName=Sammy&cardNumber=5413+++9130&cardExpiry=0125&cardToken=0eaf06f7a255b5f0f7703f&cardType=Mastercard&transactionId=36341061&avsResponse=X&cvvResponse=M&approvalCode=T4E6ST&orderNumber=INV001007&customerCode=CST1007&currency=USD&xmlHash=10d7b700eb6d291b19368a437dd19709fcadb6da36879f078a9ad4548986101a&xml=%3Cmessage%3E%3Cresponse%3E1%3C%2Fresponse%3E%3CresponseMessage%3EAPPROVED%3C%2FresponseMessage%3E%3CnoticeMessage%3ENew+Card+Stored+-+New+Customer+Created+-+New+Order+Created+-+Order+Marked+as+Paid%3C%2FnoticeMessage%3E%3Cdate%3E2023-04-14%3C%2Fdate%3E%3Ctime%3E09%3A00%3A57%3C%2Ftime%3E%3Ctype%3Epurchase%3C%2Ftype%3E%3Camount%3E100.00%3C%2Famount%3E%3Ccurrency%3EUSD%3C%2Fcurrency%3E%3CcardHolderName%3ESammy%3C%2FcardHolderName%3E%3CcardNumber%3E5413+++9130%3C%2FcardNumber%3E%3CcardExpiry%3E0125%3C%2FcardExpiry%3E%3CcardToken%3E0eaf06f7a255b5f0f7703f%3C%2FcardToken%3E%3CcardType%3EMastercard%3C%2FcardType%3E%3CtransactionId%3E36341061%3C%2FtransactionId%3E%3CavsResponse%3EX%3C%2FavsResponse%3E%3CcvvResponse%3EM%3C%2FcvvResponse%3E%3CapprovalCode%3ET4E6ST%3C%2FapprovalCode%3E%3CorderNumber%3EINV001007%3C%2ForderNumber%3E%3CcustomerCode%3ECST1007%3C%2FcustomerCode%3E%3CincludeXML%3E1%3C%2FincludeXML%3E%3C%2Fmessage%3E'

I am trying to get some attribute/value from the response, i don’t know if that would be possible since this is not a json response, this is how i am trying to get the reponse

def process_endpoint(request):
    data = request.body.amount
    print(data)

    context = {
        'response':data
    }
    
    return render(request, "payments/response.html", context)

but i keep getting this error that says AttributeError: 'bytes' object has no attribute 'amount' whenever i include json_data = request.body.amount.


Firstly, can i convert this response that i got to a json response?
I tried doing that but got an error that says

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

this is the code i used

import json 
json_data = json.loads(request.body.decode(encoding='UTF-8'))

Secondly, can i get some specific attributes/value from the response?, e.g amount, date, payment_type etc

You are correct, that’s not JSON. It’s actually an http form data format.

If you had a form for this data, you could bind to it directly.

You have:

What if you try:

    data = request.POST
    print(data)

That should give you a QueryDict object from which you can extract the data you are interested in.

1 Like
data = request.POST
print(data)

Thank you very much, this fixed it for me.