chiark / gitweb /
Log JSON responses more usefully
authorSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 18:59:38 +0000 (18:59 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 18:59:38 +0000 (18:59 +0000)
client.py

index e83f42f3f07eb08e2c15c20db288a3836983ad7e..3e554348169d457e1c3a3458db8adfffda134b50 100644 (file)
--- a/client.py
+++ b/client.py
@@ -1,4 +1,5 @@
 import calendar
+import json
 import os
 import re
 import requests
@@ -51,7 +52,13 @@ class Client:
             pr("  Response headers:")
             for k, v in rsp.headers.items():
                 pr(f"    {k}: {v}")
-            pr(f"  Response: {rsp.content!r}")
+            if 'application/json' not in rsp.headers.get('content-type'):
+                pr(f"  Response: {rsp.content!r}")
+            else:
+                pr("  Response JSON:")
+                j = rsp.json()
+                for line in json.dumps(j, indent=4).splitlines():
+                    pr("    " + line)
 
         self.log_response = log_response