From 01a99f2b252c2a4b467d26c795173a026df6d4d7 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Fri, 4 Mar 2016 01:01:16 +0530 Subject: Handle the case of correct json content-type but malformed body --- mitmproxy/flow_export.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mitmproxy/flow_export.py b/mitmproxy/flow_export.py index f5b3e2ec..49e2d704 100644 --- a/mitmproxy/flow_export.py +++ b/mitmproxy/flow_export.py @@ -56,8 +56,9 @@ def python_code(flow): data = "" if flow.request.body: - if is_json(flow.request.headers): - data = json.dumps(json.loads(flow.request.body), indent=4) + json_obj = is_json(flow.request.headers, flow.request.body) + if json_obj: + data = json.dumps(json_obj, indent=4) data = "\njson = %s\n" % data args += "\n json=json," else: @@ -81,8 +82,12 @@ def raw_request(flow): return data -def is_json(headers): +def is_json(headers, content): if headers: ct = parse_content_type(headers.get("content-type", "")) - return ct and "%s/%s" % (ct[0], ct[1]) == "application/json" + if ct and "%s/%s" % (ct[0], ct[1]) == "application/json": + try: + return json.loads(content) + except ValueError: + return False return False -- cgit v1.2.3