Permalink
Browse files

Compatible headers structure

  • Loading branch information...
1 parent 7b411a6 commit afaf8265e9ff784306a8486fa5b9d34fab5e7044 @dreid dreid committed Jun 29, 2012
Showing with 13 additions and 3 deletions.
  1. +13 −3 treq/response.py
View
@@ -1,8 +1,13 @@
import json
+import cgi
+
from twisted.internet.defer import succeed, Deferred
from twisted.internet.protocol import Protocol
+from requests.structures import CaseInsensitiveDict
+from requests.utils import get_encoding_from_headers
+
class _BodyCollector(Protocol):
def __init__(self, finished):
@@ -26,7 +31,10 @@ def __init__(self, response, method):
self._response = response
self._method = method
self.status_code = response.code
- self.headers = response.headers
+ self.headers = CaseInsensitiveDict((
+ (header, ', '.join(values)) for header, values in
+ response.headers.getAllRawHeaders()))
+ self.encoding = get_encoding_from_headers(self.headers)
@property
def content(self):
@@ -60,15 +68,17 @@ def _json_decode(data):
if self._json is not None:
return succeed(self._json)
- if self.headers.getRawHeaders('content-type') != ['application/json']:
+ content_type, params = cgi.parse_header(self.headers['content-type'])
+
+ if content_type != 'application/json':
return None
return self.content.addCallback(_json_decode)
@property
def text(self):
def _text_decode(data):
- self._text = data.decode('utf-8')
+ self._text = data.decode(self.encoding)
return self._text
if self._text is not None:

0 comments on commit afaf826

Please sign in to comment.