arduino/Arduino · error · DecodeError

Received response with content-encoding: %s, but failed to d

Error message

Received response with content-encoding: %s, but failed to decode it.

What it means

urllib3 promised a compressed response (Content-Encoding header, e.g. gzip/deflate) but the decompressor (zlib/GzipFile) raised IOError or zlib.error while decoding the body, so urllib3 wraps it in a DecodeError. The bytes on the wire are not valid data for the declared encoding.

Source

Thrown at arduino-core/src/processing/app/i18n/python/requests/packages/urllib3/response.py:164

                data = self._fp.read()
            else:
                data = self._fp.read(amt)
                if amt != 0 and not data:  # Platform-specific: Buggy versions of Python.
                    # Close the connection when no data is returned
                    #
                    # This is redundant to what httplib/http.client _should_
                    # already do.  However, versions of python released before
                    # December 15, 2012 (http://bugs.python.org/issue16298) do not
                    # properly close the connection in all cases. There is no harm
                    # in redundantly calling close.
                    self._fp.close()
                return data

            try:
                if decode_content and decoder:
                    data = decoder(data)
            except (IOError, zlib.error):
                raise DecodeError("Received response with content-encoding: %s, but "
                                  "failed to decode it." % content_encoding)

            if cache_content:
                self._body = data

            return data

        finally:
            if self._original_response and self._original_response.isclosed():
                self.release_conn()

    @classmethod
    def from_httplib(ResponseCls, r, **response_kw):
        """
        Given an :class:`httplib.HTTPResponse` instance ``r``, return a
        corresponding :class:`urllib3.response.HTTPResponse` object.

        Remaining parameters are passed to the HTTPResponse constructor, along

View on GitHub (pinned to a0df6e0e83)

Solutions

  1. Inspect the raw body: request with headers={'Accept-Encoding': 'identity'} or resp.read(decode_content=False) to see the actual bytes and find who mangles them.
  2. Fix the server/proxy to only set Content-Encoding when it actually compresses the payload.
  3. If you decompress manually, pass decode_content=False so urllib3 does not also try to decode.
  4. Retry the request — if it is intermittent it may be a truncated response from a proxy; add error handling around read().

Example fix

// before
r = http.request('GET', url)
body = r.read()  # DecodeError: claims gzip but isn't
// after
r = http.request('GET', url, headers={'Accept-Encoding': 'identity'})
body = r.read()
Defensive patterns

Strategy: try-catch

Validate before calling

ce = resp.headers.get('Content-Encoding')
if ce and ce.strip().lower() not in ('', 'identity'):
    # inspect raw bytes before decoding
    raw = resp.read(decode_content=False)
    if ce == 'gzip' and raw[:2] != b'\x1f\x8b':
        raise ValueError('Server sent Content-Encoding: gzip without gzip data')

Try / catch

from urllib3.exceptions import DecodeError
try:
    body = resp.read()
except DecodeError as e:
    log.warning('Bad content-encoding from %s: %s', resp.url, e)
    body = resp.read(decode_content=False)  # fall back to raw bytes

Prevention

When it happens

Trigger: A server/proxy sets 'Content-Encoding: gzip' but sends uncompressed, truncated, or corrupt bytes; a double-compression bug ('Content-Encoding: gzip, gzip' with single-encoded body); streaming reads (resp.read()) that hit the malformed chunk mid-stream.

Common situations: Buggy middlewares/CDNs mangling bodies; responses consumed after a failed request; custom WSGI apps adding the header without compressing; resuming a truncated download; requests' automatic decode_content interacting with manual decompression.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


AI-assisted analysis of arduino/Arduino@a0df6e0e83 (2026-09-06). Data as JSON: /api/errors/ad9b0dd0fd71740b. Report an issue: GitHub.