Skip to content

Instantly share code, notes, and snippets.

@Tranquility2
Forked from defrex/pretty_request.py
Last active December 23, 2019 12:53
Show Gist options
  • Save Tranquility2/2c3edd2696a2f3af94fea6780d2d2b3b to your computer and use it in GitHub Desktop.
Save Tranquility2/2c3edd2696a2f3af94fea6780d2d2b3b to your computer and use it in GitHub Desktop.
A simple function to print a Django request the way requests are meant to be printed.
class AnsiColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def pretty_request(request):
headers = ''
for header, value in request.META.items():
if not header.startswith('HTTP'):
continue
header = '-'.join([h.capitalize() for h in header[5:].lower().split('_')])
headers += '{}: {}\n'.format(header, value)
return (
f'{AnsiColors.OKBLUE + request.method + AnsiColors.ENDC}\n'
f"Content-Length: {request.META['CONTENT_LENGTH']}\n"
f"Content-Type: {request.META['CONTENT_TYPE']}\n"
f'{AnsiColors.OKGREEN}Version: {request.version + AnsiColors.ENDC}\n'
f'{headers}'
f'{request.body if request.body else "[Empty Body]"}'
)
@Tranquility2
Copy link
Author

  1. Updated to python 3
  2. Added Colors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment