Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for brotli decoding #5783

Merged
merged 4 commits into from Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions HISTORY.md
Expand Up @@ -5,6 +5,8 @@ dev
---

- \[Short description of non-trivial change.\]
- Requests Brotli compression, if either the `brotli` or `brotlicffi` package
is installed.

2.25.1 (2020-12-16)
-------------------
Expand Down
3 changes: 3 additions & 0 deletions docs/community/faq.rst
Expand Up @@ -11,6 +11,9 @@ Encoded Data?
Requests automatically decompresses gzip-encoded responses, and does
its best to decode response content to unicode when possible.

When either the `brotli <https://pypi.org/project/Brotli/>`_ or `brotlicffi <https://pypi.org/project/brotlicffi/>`_
package is installed, requests also decodes Brotli-encoded responses.

You can get direct access to the raw response (and even the socket),
if needed as well.

Expand Down
3 changes: 3 additions & 0 deletions docs/user/quickstart.rst
Expand Up @@ -128,6 +128,9 @@ You can also access the response body as bytes, for non-text requests::

The ``gzip`` and ``deflate`` transfer-encodings are automatically decoded for you.

The ``br`` transfer-encoding is automatically decoded for you if a Brotli library
like `brotli <https://pypi.org/project/brotli>`_ or `brotlicffi <https://pypi.org/project/brotli>`_ is installed.

For example, to create an image from binary data returned by a request, you can
use the following code::

Expand Down
8 changes: 7 additions & 1 deletion requests/utils.py
Expand Up @@ -20,6 +20,7 @@
import warnings
import zipfile
from collections import OrderedDict
from urllib3.util import make_headers

from .__version__ import __version__
from . import certs
Expand All @@ -41,6 +42,11 @@

DEFAULT_PORTS = {'http': 80, 'https': 443}

# Ensure that ', ' is used to preserve previous delimiter behavior.
DEFAULT_ACCEPT_ENCODING = ", ".join(
re.split(r",\s*", make_headers(accept_encoding=True)["accept-encoding"])
)


if sys.platform == 'win32':
# provide a proxy_bypass version on Windows without DNS lookups
Expand Down Expand Up @@ -820,7 +826,7 @@ def default_headers():
"""
return CaseInsensitiveDict({
'User-Agent': default_user_agent(),
'Accept-Encoding': ', '.join(('gzip', 'deflate')),
'Accept-Encoding': DEFAULT_ACCEPT_ENCODING,
'Accept': '*/*',
'Connection': 'keep-alive',
})
Expand Down