Python - unhashable type error in urllib2

12,085

Solution 1

data is suppossed to be a "a buffer in the standard application/x-www-form-urlencoded format.", not a dict.

Before you pass the data dict in do data = urllib.urlencode(data), so you get the correct format from your dict.

Solution 2

You can also use the following if you are using json.

json.dumps(data)

Remember: urlencode can encode a dict, but not a string. The output of json.dumps is a string.

Share:
12,085
orokusaki
Author by

orokusaki

I'm Michael Angeletti. I am a Python / Django developer, specializing in SaaS applications.

Updated on June 04, 2022

Comments

  • orokusaki
    orokusaki almost 2 years
    >> url = 'https://test.authorize.net/gateway/transact.dll'
    >> data = {'x_login': 'abc123', 'x_type': 'AUTH_CAPTURE', 'x_card_num': '4444333322221103', 'x_amount': '50.75', 'x_tran_key
    ': 'abc123', 'x_version': '3.1', 'x_delim_char': '|', 'x_exp_date': '022012', 'x_delim_data': 'TRUE'}
    >> 
    >> urllib2.urlopen(url, data)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "gateways\base.py", line 81, in dispatch
        return gw_method(self, *args, **kwargs)
      File "gateways\decorators.py", line 17, in wrapper
        method(*args, **kwargs)
      File "gateways\authorize_net.py", line 39, in auth_capture
        return self.post_data(data)
      File "gateways\authorize_net.py", line 43, in post_data
        raw_response = urllib2.urlopen(self.get_endpoint(), data)
      File "C:\Python26\lib\urllib2.py", line 124, in urlopen
        return _opener.open(url, data, timeout)
      File "C:\Python26\lib\urllib2.py", line 389, in open
        response = self._open(req, data)
      File "C:\Python26\lib\urllib2.py", line 407, in _open
        '_open', req)
      File "C:\Python26\lib\urllib2.py", line 367, in _call_chain
        result = func(*args)
      File "C:\Python26\lib\urllib2.py", line 1154, in https_open
        return self.do_open(httplib.HTTPSConnection, req)
      File "C:\Python26\lib\urllib2.py", line 1118, in do_open
        h.request(req.get_method(), req.get_selector(), req.data, headers)
      File "C:\Python26\lib\httplib.py", line 898, in request
        self._send_request(method, url, body, headers)
      File "C:\Python26\lib\httplib.py", line 938, in _send_request
        self.send(body)
      File "C:\Python26\lib\httplib.py", line 743, in send
        self.sock.sendall(str)
      File "C:\Python26\lib\ssl.py", line 203, in sendall
        v = self.send(data[count:])
    TypeError: unhashable type
    

    I can't figure out what caused this error.