MCPcopy
hub / github.com/psf/requests / json

Method json

src/requests/models.py:1087–1120  ·  src/requests/models.py::Response.json

r"""Decodes the JSON response body (if any) as a Python object. This may return a dictionary, list, etc. depending on what is in the response. :param \*\*kwargs: Optional arguments that ``json.loads`` takes. :raises requests.exceptions.JSONDecodeError: If the response body

(self, **kwargs: Any)

Source from the content-addressed store, hash-verified

1085 return content
1086
1087 def json(self, **kwargs: Any) -> Any:
1088 rclass="st">"""Decodes the JSON response body (if any) as a Python object.
1089
1090 This may return a dictionary, list, etc. depending on what is in the response.
1091
1092 :param \*\*kwargs: Optional arguments that ``json.loads`` takes.
1093 :raises requests.exceptions.JSONDecodeError: If the response body does not
1094 contain valid json.
1095 class="st">"""
1096
1097 if not self.encoding and self.content and len(self.content) > 3:
1098 class="cm"># No encoding set. JSON RFC 4627 section 3 states we should expect
1099 class="cm"># UTF-8, -16 or -32. Detect which one to use; If the detection or
1100 class="cm"># decoding fails, fall back to `self.text` (using charset_normalizer to make
1101 class="cm"># a best guess).
1102 encoding = guess_json_utf(self.content)
1103 if encoding is not None:
1104 try:
1105 return complexjson.loads(self.content.decode(encoding), **kwargs)
1106 except UnicodeDecodeError:
1107 class="cm"># Wrong UTF codec detected; usually because it's not UTF-8
1108 class="cm"># but some other 8-bit codec. This is an RFC violation,
1109 class="cm"># and the server didn't bother to tell us what codec *was*
1110 class="cm"># used.
1111 pass
1112 except JSONDecodeError as e:
1113 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
1114
1115 try:
1116 return complexjson.loads(self.text, **kwargs)
1117 except JSONDecodeError as e:
1118 class="cm"># Catch JSON-related errors and raise as requests.JSONDecodeError
1119 class="cm"># This aliases json.JSONDecodeError and simplejson.JSONDecodeError
1120 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
1121
1122 @property
1123 def links(self) -> dict[str, dict[str, str]]:

Calls 1

guess_json_utfFunction · 0.85