1

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?

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44

1 Answers1

2
from pprint import pprint

pprint(dict(response.headers))
  • Thank you! I finally got it to work using the exact code you posted. This displays the indented text as plain text in Jupyter NB. I am wondering if there is a way to have colored text output using built-in Python? – Rich Lysakowski PhD Jun 18 '23 at 17:21
  • U can use "colorama" ```python from colorama import Fore, Back, Style # Print colored text print(f"{Fore.RED}This is red text.") print(f"{Back.GREEN}This has a green background.") print(f"{Style.BRIGHT}This is bright text.") print(f"{Fore.YELLOW}{Back.BLUE}You can combine colors.{Style.RESET_ALL}") # Reset colors to default print(f"{Style.RESET_ALL}This is back to normal.") ``` U can use it with pprint if u whant ```python print(f"{Fore.RED}{pformat(dict(response.headers))}{Style.RESET_ALL}") ``` – LtGenFlower Jun 18 '23 at 18:32