Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - unhashable type error in urllib2

Tags:

>> 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.

like image 722
orokusaki Avatar asked Oct 08 '10 18:10

orokusaki


People also ask

How do I fix Unhashable type list in Python?

This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key. The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.

How do you fix a TypeError Unhashable type dict?

The Python "TypeError: unhashable type: 'dict'" occurs when we use a dictionary as a key in another dictionary or as an element in a set . To solve the error, use a frozenset instead, or convert the dictionary into a JSON string before using it as a key.

What is Unhashable type error in Python?

TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed.

What does Unhashable type mean?

The message “TypeError: unhashable type” appears in a Python program when you try to use a data type that is not hashable in a place in your code that requires hashable data. For example, as an item of a set or as a key of a dictionary.


1 Answers

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.

like image 87
Sam Dolan Avatar answered Oct 16 '22 10:10

Sam Dolan