Last active
September 8, 2023 14:33
-
-
Save defrex/6140951 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | |
'{method} HTTP/1.1\n' | |
'Content-Length: {content_length}\n' | |
'Content-Type: {content_type}\n' | |
'{headers}\n\n' | |
'{body}' | |
).format( | |
method=request.method, | |
content_length=request.META['CONTENT_LENGTH'], | |
content_type=request.META['CONTENT_TYPE'], | |
headers=headers, | |
body=request.body, | |
) |
Much obliged!
replaced body
with pprint.pformat(json.loads(request.body), indent=2)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!