Explorar o código

Improve JSON output formatting of webhook receiver

Jeremy Stretch %!s(int64=5) %!d(string=hai) anos
pai
achega
c083b862a7
Modificáronse 1 ficheiros con 5 adicións e 2 borrados
  1. 5 2
      netbox/extras/management/commands/webhook_receiver.py

+ 5 - 2
netbox/extras/management/commands/webhook_receiver.py

@@ -1,3 +1,4 @@
+import json
 import sys
 import sys
 from http.server import HTTPServer, BaseHTTPRequestHandler
 from http.server import HTTPServer, BaseHTTPRequestHandler
 
 
@@ -47,8 +48,10 @@ class WebhookHandler(BaseHTTPRequestHandler):
         # Print the request body (if any)
         # Print the request body (if any)
         content_length = self.headers.get('Content-Length')
         content_length = self.headers.get('Content-Length')
         if content_length is not None:
         if content_length is not None:
-            body = self.rfile.read(int(content_length))
-            print(body.decode('utf-8'))
+            body = self.rfile.read(int(content_length)).decode('utf-8')
+            if self.headers.get('Content-Type') == 'application/json':
+                body = json.loads(body)
+                print(json.dumps(body, indent=4))
         else:
         else:
             print('(No body)')
             print('(No body)')