if prefetch:
# Save the response.
self.response.content
-
+
if self.config.get('danger_mode'):
self.response.raise_for_status()
return True
- def iter_content(self, chunk_size=10 * 1024, decode_unicode=None):
+ def iter_content(self, chunk_size=10 * 1024, decode_unicode=False):
"""Iterates over the response data. This avoids reading the content
at once into memory for large responses. The chunk size is the number
of bytes it should read into memory. This is not necessarily the
break
yield chunk
self._content_consumed = True
-
+
def generate_chunked():
resp = self.raw._original_response
fp = resp.fp
elif 'deflate' in self.headers.get('content-encoding', ''):
gen = stream_decompress(gen, mode='deflate')
- if decode_unicode is None:
- decode_unicode = self.config.get('decode_unicode')
-
if decode_unicode:
gen = stream_decode_response_unicode(gen, self)
@property
def content(self):
- """Content of the response, in bytes or unicode
- (if available).
- """
+ """Content of the response, in bytes."""
if self._content is None:
# Read the contents.
except AttributeError:
self._content = None
- content = self._content
+ self._content_consumed = True
+ return self._content
- # Decode unicode content.
- if self.config.get('decode_unicode'):
- # Try charset from content-type
+ @property
+ def text(self):
+ """Content of the response, in unicode."""
- if self.encoding:
- try:
- content = unicode(content, self.encoding)
- except UnicodeError:
- pass
+ # Try charset from content-type
- # Fall back:
- try:
- content = unicode(content, self.encoding, errors='replace')
- except TypeError:
- pass
+ content = u''
+
+ try:
+ content = unicode(self.content, self.encoding)
+ except UnicodeError:
+ pass
+
+ # Try to fall back:
+ try:
+ content = unicode(content, self.encoding, errors='replace')
+ except TypeError:
+ pass
- self._content_consumed = True
return content