accept and support compressed response from api, POST with gzip encoded body is not supported in django/wsgi

This commit is contained in:
j 2012-08-22 00:54:04 +02:00
parent ebf3b691b2
commit ec04c29bcd

View file

@ -4,6 +4,8 @@
from __future__ import with_statement from __future__ import with_statement
import cookielib import cookielib
import gzip
import StringIO
import urllib2 import urllib2
from .utils import json from .utils import json
@ -56,12 +58,16 @@ class API(object):
def _json_request(self, url, form): def _json_request(self, url, form):
result = {} result = {}
try: try:
request = urllib2.Request(str(url))
body = str(form) body = str(form)
request = urllib2.Request(str(url))
request.add_header('Content-type', form.get_content_type()) request.add_header('Content-type', form.get_content_type())
request.add_header('Content-length', len(body)) request.add_header('Content-Length', str(len(body)))
request.add_header('Accept-Encoding', 'gzip, deflate')
request.add_data(body) request.add_data(body)
result = self._opener.open(request).read().strip() f = self._opener.open(request)
result = f.read()
if f.headers.get('content-encoding', None) == 'gzip':
result = gzip.GzipFile(fileobj=StringIO.StringIO(result)).read()
result = result.decode('utf-8') result = result.decode('utf-8')
return json.loads(result) return json.loads(result)
except urllib2.HTTPError, e: except urllib2.HTTPError, e: