Skip to content

Commit 2cf94a2

Browse files
committed
Fix for unicode encode bug.
1 parent 3446963 commit 2cf94a2

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

gcm/gcm.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ def group_response(response, registration_ids, key):
4848
return grouping
4949

5050

51+
def urlencode_utf8(params):
52+
"""
53+
UTF-8 safe variant of urllib.urlencode.
54+
http://stackoverflow.com/a/8152242
55+
"""
56+
57+
if hasattr(params, 'items'):
58+
params = params.items()
59+
60+
params = (
61+
'='.join((
62+
urllib.quote_plus(k.encode('utf8'), safe='/'),
63+
urllib.quote_plus(v.encode('utf8'), safe='/')
64+
)) for k, v in params
65+
)
66+
67+
return '&'.join(params)
68+
69+
5170
class GCM(object):
5271

5372
# Timeunit is milliseconds.
@@ -133,7 +152,7 @@ def make_request(self, data, is_json=True):
133152
headers['Content-Type'] = 'application/json'
134153

135154
if not is_json:
136-
data = urllib.urlencode(data)
155+
data = urlencode_utf8(data)
137156
req = urllib2.Request(self.url, data, headers)
138157

139158
try:

0 commit comments

Comments
 (0)