2010-10-08 29 views
16
>> 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 

Tôi không thể tìm ra nguyên nhân gây ra lỗi này.Python - lỗi loại không thể sửa được trong urllib2

Trả lời

35

data được giả sử là "bộ đệm theo định dạng ứng dụng tiêu chuẩn/x-www-form-urlencoded", không phải là một dict.

Trước khi bạn chuyển dict dữ liệu vào data = urllib.urlencode(data), do đó bạn có định dạng chính xác từ dict của bạn.

4

Bạn cũng có thể sử dụng phần sau nếu bạn đang sử dụng json.

json.dumps(data) 

Hãy nhớ: urlencode có thể mã hóa một dict chứ không phải chuỗi. Đầu ra của json.dumps là một chuỗi.

Các vấn đề liên quan