From ec61cfddfd284a13d7bcf2807ba2e95c1a7f1d51 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Thu, 3 Mar 2016 06:41:26 +0530 Subject: [PATCH] Use json instead of contentviews --- mitmproxy/flow_export.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/mitmproxy/flow_export.py b/mitmproxy/flow_export.py index b3bef62f7..f5b3e2ec1 100644 --- a/mitmproxy/flow_export.py +++ b/mitmproxy/flow_export.py @@ -1,8 +1,9 @@ +import json import urllib from textwrap import dedent import netlib.http -from . import contentviews +from netlib.utils import parse_content_type def curl_command(flow): @@ -55,14 +56,9 @@ def python_code(flow): data = "" if flow.request.body: - cv = contentviews.get_content_view( - viewmode=contentviews.get("Auto"), - data=flow.request.body, - headers=flow.request.headers, - ) - - if cv[0] == "JSON": - data = "\njson = %s\n" % "\n".join(l[0][1] for l in cv[1]) + if is_json(flow.request.headers): + data = json.dumps(json.loads(flow.request.body), indent=4) + data = "\njson = %s\n" % data args += "\n json=json," else: data = "\ndata = '''%s'''\n" % flow.request.body @@ -83,3 +79,10 @@ def python_code(flow): def raw_request(flow): data = netlib.http.http1.assemble_request(flow.request) return data + + +def is_json(headers): + if headers: + ct = parse_content_type(headers.get("content-type", "")) + return ct and "%s/%s" % (ct[0], ct[1]) == "application/json" + return False