Skip to content

Instantly share code, notes, and snippets.

@anupsabraham
Last active December 5, 2019 13:04
Show Gist options
  • Save anupsabraham/b5148171496e4d69c71aec06c379701e to your computer and use it in GitHub Desktop.
Save anupsabraham/b5148171496e4d69c71aec06c379701e to your computer and use it in GitHub Desktop.
Pretty print request(json/xml/form-data) and response in python
import re
import json
from urllib import unquote_plus
from xml.dom import minidom
from xml.parsers.expat import ExpatError
def remove_blanks_xml(node):
"""Remove blank texts from xml"""
for child_node in node.childNodes:
if child_node.nodeType == minidom.Node.TEXT_NODE:
if child_node.nodeValue:
child_node.nodeValue = child_node.nodeValue.strip()
elif child_node.nodeType == minidom.Node.ELEMENT_NODE:
remove_blanks_xml(child_node)
def pretty_print_request(req):
""" For pretty print request """
http_version = req.raw.version
version_str = str(http_version/10) + "." + str(http_version%10)
if req.request.method == "GET":
request_body = req.request.body if req.request.body else ""
elif req.request.method == "POST":
if req.request.body:
try:
# Try to decode request body as a json.
request_body_obj = json.loads(req.request.body)
request_body = json.dumps(request_body_obj, indent=2)
except ValueError:
# Not a json object
request_body = ""
if not request_body:
# if not json, try to decode request body as an xml
try:
request_body_xml = minidom.parseString(req.request.body)
remove_blanks_xml(request_body_xml)
request_body_xml.normalize()
request_body = request_body_xml.toprettyxml(indent=' ', newl='\n')
except ExpatError:
# Not an xml object
request_body = ""
if not request_body:
# If not json or xml, treat request body as post data
body_split = req.request.body.split("&")
param_bodies = []
for each_parameter in body_split:
param, value = each_parameter.split("=")
decoded_value = unquote_plus(value)
try:
temp_value = json.loads(decoded_value)
value = json.dumps(temp_value, indent=2)
except ValueError:
pass
param_body = "{} = {}".format(param, value)
param_bodies.append(param_body)
request_body = "\n".join(param_bodies)
else:
request_body = ""
print('{start}\nHTTP/{version} {method_path}\nHOST: {host}\n\n{req_headers}\n\nRequest Body:\n{req_body}\n{end}'.format(
start='-----------REQUEST START-----------',
version=str(version_str), method_path=req.request.method + ' ' + req.request.path_url,
host=re.search(r"^(https?:\/\/[\w\.-]+(:\d{,5})?\/)", req.request.url).group(0),
req_headers='\n'.join('{}: {}'.format(k, v) for k, v in req.request.headers.items()),
req_body=request_body,
end='------------REQUEST END------------'
))
def pretty_print_response(req):
"""Pretty print response"""
http_version = req.raw.version
version_str = str(http_version/10) + "." + str(http_version%10)
try:
temp_response_content = req.json()
response_content = json.dumps(temp_response_content, indent=2)
except ValueError:
response_content = req.content
print('{start}\nHTTP/{version}\nStatus Code: {response_status_code}\n\n{resp_headers}\n\nResponse Content:\n{resp_content}\n{end}'.format(
start='-----------RESPONSE START-----------',
version=str(version_str), response_status_code=req.status_code,
resp_headers='\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
resp_content=response_content,
end='------------RESPONSE END------------'
))
def pprint_request_response(req):
"""One method to rule them all"""
pretty_print_request(req)
print("\n")
pretty_print_response(req)
import requests
### XML DATA ###
xml_body = """<?xml version="1.0" ?><catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>"""
xml_req = requests.post("https://example.com", data=xml_body)
pprint_request_response(xml_req)
### JSON DATA ###
json_body = json.loads("""{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}""")
json_req = requests.post("https://example.com", json=json_body)
pprint_request_response(json_req)
### FORM DATA ###
form_data_body = {
'param1': "value1",
'param2': "value2"
}
form_data_req = requests.post("https://example.com", data=form_data_body)
pprint_request_response(form_data_req)
### GET REQUEST ###
get_req = requests.get("https://example.com", params=form_data_body)
pprint_request_response(get_req)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment