<p dir="auto">Requests fails on the URL <code class="notranslate">http://www.whatbird.com/forum/index.php?/gallery/image/291517-foo/</code>, which is a 301 redirect to</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="http://www.whatbird.com/forum/index.php?/gallery/image/291517-title-paused-jewel-allens-hummingbird-a-backyard-bird-painting-in-oil-by-camille-engel/"><pre class="notranslate"><code class="notranslate">http://www.whatbird.com/forum/index.php?/gallery/image/291517-title-paused-jewel-allens-hummingbird-a-backyard-bird-painting-in-oil-by-camille-engel/
</code></pre></div>
<p dir="auto">. The issue seems to be that the server's initial 301 response has a header falsely claiming that the response body (a simple HTML page) is gzipped, when it's actually uncompressed.</p>
<p dir="auto">When resolving redirects, Requests does (in <code class="notranslate">requests.sessions.resolve_redirects</code>):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="resp.content  # Consume socket so it can be released"><pre class="notranslate"><code class="notranslate">resp.content  # Consume socket so it can be released
</code></pre></div>
<p dir="auto">which attempts to decode</p>
<p dir="auto">One could legitimately say this is the server's problem. However, conceptually, why decode the response body of a redirect, which won't get returned? Other programs (Chromium, Firefox, <code class="notranslate">curl</code>) don't do this. For example, <code class="notranslate">curl</code> gives an error, as expected, when not following redirects:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ curl --compressed 'http://www.whatbird.com/forum/index.php?/gallery/image/291517-foo/'
curl: (61) Error while processing content unencoding: invalid code lengths set"><pre class="notranslate"><code class="notranslate">$ curl --compressed 'http://www.whatbird.com/forum/index.php?/gallery/image/291517-foo/'
curl: (61) Error while processing content unencoding: invalid code lengths set
</code></pre></div>
<p dir="auto">whereas it works if you add the <code class="notranslate">--location</code> flag (follow redirects).</p>
<h1 dir="auto">Example of error</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Python 3.3.2+ (default, Oct  9 2013, 14:56:03) 
[GCC 4.8.1] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import requests ; requests.get('http://www.whatbird.com/forum/index.php?/gallery/image/291517-foo/')
Traceback (most recent call last):
  File &quot;./requests/packages/urllib3/response.py&quot;, line 199, in read
    data = self._decoder.decompress(data)
zlib.error: Error -3 while decompressing data: incorrect header check

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File &quot;./requests/models.py&quot;, line 629, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File &quot;./requests/packages/urllib3/response.py&quot;, line 236, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File &quot;./requests/packages/urllib3/response.py&quot;, line 204, in read
    e)
requests.packages.urllib3.exceptions.DecodeError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;./requests/api.py&quot;, line 55, in get
    return request('get', url, **kwargs)
  File &quot;./requests/api.py&quot;, line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File &quot;./requests/sessions.py&quot;, line 393, in request
    resp = self.send(prep, **send_kwargs)
  File &quot;./requests/sessions.py&quot;, line 496, in send
    r = adapter.send(request, **kwargs)
  File &quot;./requests/adapters.py&quot;, line 391, in send
    r.content
  File &quot;./requests/models.py&quot;, line 691, in content
    self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
  File &quot;./requests/models.py&quot;, line 634, in generate
    raise ContentDecodingError(e)
requests.exceptions.ContentDecodingError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check',))"><pre class="notranslate"><code class="notranslate">Python 3.3.2+ (default, Oct  9 2013, 14:56:03) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import requests ; requests.get('http://www.whatbird.com/forum/index.php?/gallery/image/291517-foo/')
Traceback (most recent call last):
  File "./requests/packages/urllib3/response.py", line 199, in read
    data = self._decoder.decompress(data)
zlib.error: Error -3 while decompressing data: incorrect header check

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./requests/models.py", line 629, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "./requests/packages/urllib3/response.py", line 236, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "./requests/packages/urllib3/response.py", line 204, in read
    e)
requests.packages.urllib3.exceptions.DecodeError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "./requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "./requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "./requests/sessions.py", line 393, in request
    resp = self.send(prep, **send_kwargs)
  File "./requests/sessions.py", line 496, in send
    r = adapter.send(request, **kwargs)
  File "./requests/adapters.py", line 391, in send
    r.content
  File "./requests/models.py", line 691, in content
    self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
  File "./requests/models.py", line 634, in generate
    raise ContentDecodingError(e)
requests.exceptions.ContentDecodingError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check',))
</code></pre></div>
<p dir="auto">See this issue on Requests: <a href="https://github.com/kennethreitz/requests/issues/1472">https://github.com/kennethreitz/requests/issues/1472</a></p>
<p dir="auto">This is the truncated traceback:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="...
/project/virtualenv/lib/python2.6/requests/packages/urllib3/response.py&quot;, line 188, in read
    &quot;failed to decode it.&quot; % content_encoding)
DecodeError: Received response with content-encoding: gzip, but failed to decode it."><pre class="notranslate"><code class="notranslate">...
/project/virtualenv/lib/python2.6/requests/packages/urllib3/response.py", line 188, in read
    "failed to decode it." % content_encoding)
DecodeError: Received response with content-encoding: gzip, but failed to decode it.
</code></pre></div>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/schlamar/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/schlamar">@schlamar</a></p>