I am trying to pretty print the http response coming back from Python Requests library in a fully Pythonic way without using any packages not built into the Python Standard Library. I am getting the JSON run around.
What I tried:
I tried loading the response.headers into a JSON string using json.loads(), and then indenting the output using json.dumps(), as follows,
import json
response_json = json.loads(response.headers)
pretty_response = json.dumps(response_json, indent=4)
print(pretty_response)
but I get the following error:
TypeError Traceback (most recent call last)
Cell In[21], line 2
1 import json
----> 2 response_json = json.loads(response.headers)
4 pretty_response = json.dumps(response_json, indent=4)
5 print(pretty_response)
File c:\ProgramData\Anaconda3\envs\webscrapers\lib\json\__init__.py:341, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
339 else:
340 if not isinstance(s, (bytes, bytearray)):
--> 341 raise TypeError(f'the JSON object must be str, bytes or bytearray, '
342 f'not {s.__class__.__name__}')
343 s = s.decode(detect_encoding(s), 'surrogatepass')
345 if "encoding" in kw:
TypeError: the JSON object must be str, bytes or bytearray, not CaseInsensitiveDict
I also tried:
First installing and importing the rich
package, and then using a module within the rich
package for Rich Text Formatting output to terminals. The print_json() function is supposed to pretty print json:
import json
import rich
rich.print_json(dict(response.headers))
but I get an error:
TypeError: json must be str. Did you mean print_json(data={'Date': 'Sun, 18 Jun 2023 16:22:08 GMT', ...}
I finally got it to work by installing and importing the rich
package and using some hints from How to pretty print nested dictionaries? and Requests: Pretty print http response headers.
import rich
rich.pretty.pprint(dict(response.headers))
However, it took some time to figure out the correct syntax, because the rich.pretty.pprint help()
documentation in Jupyter did not have detailed examples for this use case. While rich
is a very nice package, it has a significant learning curve. More importantly, it is not a native Python built-in solution.
How can this be done simply using Python with no 3rd party package installations?