|
|
@ -29,7 +29,9 @@ from .auth import HTTPBasicAuth |
|
|
|
from .cookies import cookiejar_from_dict, get_cookie_header, _copy_cookie_jar |
|
|
|
from .exceptions import ( |
|
|
|
HTTPError, MissingSchema, InvalidURL, ChunkedEncodingError, |
|
|
|
ContentDecodingError, ConnectionError, StreamConsumedError) |
|
|
|
ContentDecodingError, ConnectionError, StreamConsumedError, |
|
|
|
InvalidJSONError) |
|
|
|
from .exceptions import JSONDecodeError as RequestsJSONDecodeError |
|
|
|
from ._internal_utils import to_native_string, unicode_is_ascii |
|
|
|
from .utils import ( |
|
|
|
guess_filename, get_auth_from_url, requote_uri, |
|
|
@ -38,7 +40,7 @@ from .utils import ( |
|
|
|
from .compat import ( |
|
|
|
Callable, Mapping, |
|
|
|
cookielib, urlunparse, urlsplit, urlencode, str, bytes, |
|
|
|
is_py2, chardet, builtin_str, basestring) |
|
|
|
is_py2, chardet, builtin_str, basestring, JSONDecodeError) |
|
|
|
from .compat import json as complexjson |
|
|
|
from .status_codes import codes |
|
|
|
|
|
|
@ -466,7 +468,12 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): |
|
|
|
# urllib3 requires a bytes-like body. Python 2's json.dumps |
|
|
|
# provides this natively, but Python 3 gives a Unicode string. |
|
|
|
content_type = 'application/json' |
|
|
|
body = complexjson.dumps(json) |
|
|
|
|
|
|
|
try: |
|
|
|
body = complexjson.dumps(json, allow_nan=False) |
|
|
|
except ValueError as ve: |
|
|
|
raise InvalidJSONError(ve, request=self) |
|
|
|
|
|
|
|
if not isinstance(body, bytes): |
|
|
|
body = body.encode('utf-8') |
|
|
|
|
|
|
@ -726,7 +733,7 @@ class Response(object): |
|
|
|
|
|
|
|
@property |
|
|
|
def apparent_encoding(self): |
|
|
|
"""The apparent encoding, provided by the chardet library.""" |
|
|
|
"""The apparent encoding, provided by the charset_normalizer or chardet libraries.""" |
|
|
|
return chardet.detect(self.content)['encoding'] |
|
|
|
|
|
|
|
def iter_content(self, chunk_size=1, decode_unicode=False): |
|
|
@ -840,7 +847,7 @@ class Response(object): |
|
|
|
"""Content of the response, in unicode. |
|
|
|
|
|
|
|
If Response.encoding is None, encoding will be guessed using |
|
|
|
``chardet``. |
|
|
|
``charset_normalizer`` or ``chardet``. |
|
|
|
|
|
|
|
The encoding of the response content is determined based solely on HTTP |
|
|
|
headers, following RFC 2616 to the letter. If you can take advantage of |
|
|
@ -877,13 +884,14 @@ class Response(object): |
|
|
|
r"""Returns the json-encoded content of a response, if any. |
|
|
|
|
|
|
|
:param \*\*kwargs: Optional arguments that ``json.loads`` takes. |
|
|
|
:raises ValueError: If the response body does not contain valid json. |
|
|
|
:raises requests.exceptions.JSONDecodeError: If the response body does not |
|
|
|
contain valid json. |
|
|
|
""" |
|
|
|
|
|
|
|
if not self.encoding and self.content and len(self.content) > 3: |
|
|
|
# No encoding set. JSON RFC 4627 section 3 states we should expect |
|
|
|
# UTF-8, -16 or -32. Detect which one to use; If the detection or |
|
|
|
# decoding fails, fall back to `self.text` (using chardet to make |
|
|
|
# decoding fails, fall back to `self.text` (using charset_normalizer to make |
|
|
|
# a best guess). |
|
|
|
encoding = guess_json_utf(self.content) |
|
|
|
if encoding is not None: |
|
|
@ -897,7 +905,16 @@ class Response(object): |
|
|
|
# and the server didn't bother to tell us what codec *was* |
|
|
|
# used. |
|
|
|
pass |
|
|
|
return complexjson.loads(self.text, **kwargs) |
|
|
|
|
|
|
|
try: |
|
|
|
return complexjson.loads(self.text, **kwargs) |
|
|
|
except JSONDecodeError as e: |
|
|
|
# Catch JSON-related errors and raise as requests.JSONDecodeError |
|
|
|
# This aliases json.JSONDecodeError and simplejson.JSONDecodeError |
|
|
|
if is_py2: # e is a ValueError |
|
|
|
raise RequestsJSONDecodeError(e.message) |
|
|
|
else: |
|
|
|
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) |
|
|
|
|
|
|
|
@property |
|
|
|
def links(self): |
|
|
|