I am trying to execute the below python function to check the status of an invoice from an external API:

def getInvoice(inv_id):
    response = requests.get("https://api.example.com/invoices/"+str(inv_id)+"?access_token=123456789").json()
    return response

print(getInvoice("5b5f60384b3f"))

When I try to call the function, I keep receiving the following error:

 Traceback (most recent call last):
  File "C:\Users\user\Desktop\app\db.py", line 82, in <module>
    print(getInvoice("5b5f60384b3f"))
  File "C:\Users\user\Desktop\app\db.py", line 79, in getInvoice
    response = requests.get("https://api.example.com/invoices/"+str(inv_id)+"?access_token=123456789").json()
TypeError: unhashable type: 'set'
[Finished in 3.0s]

I don't understand why this keeps happening, or where the 'set' is? Any help is much appreciated.

2 Answers

0
Coder On

Looks like your url is not returning json response. I suggest you to dubug the code by reading the response as text and check what it really contains.

import urllib.request

contents = urllib.request.urlopen("https://api.example.com/invoices/"+str(inv_id)+"?access_token=123456789").read()

print(contents);
0
Ivan Vinogradov On

As it was mentioned, looks like server does not reply with JSON.

Change getInvoice function and examine server response:

def getInvoice(inv_id):
    url = "https://api.example.com/invoices/{}?access_token=123456789".format(inv_id)
    response = requests.get(url)
    response.raise_for_status()  # you will get an exception if HTTP status code != 200

    try:
        return response.json()
    except:  # for debugging purposes only
        print("Failed to obtain JSON response. Response contents:\n{}".format(response.text))
        raise