From b4f301f44cef2763fa7986288083f94fddacd714 Mon Sep 17 00:00:00 2001 From: alufers Date: Fri, 29 Dec 2023 18:20:49 +0100 Subject: [PATCH] Only parse requests and responses as msgpack if they were not JSON --- mitmproxy2swagger/mitmproxy2swagger.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/mitmproxy2swagger/mitmproxy2swagger.py b/mitmproxy2swagger/mitmproxy2swagger.py index d818f4d..e2e2f74 100644 --- a/mitmproxy2swagger/mitmproxy2swagger.py +++ b/mitmproxy2swagger/mitmproxy2swagger.py @@ -242,12 +242,13 @@ def main(override_args: Optional[Sequence[str]] = None): except json.decoder.JSONDecodeError: pass - # try to parse the body as msgpack - try: - body_val = msgpack.loads(req.get_request_body()) - content_type = "application/msgpack" - except Exception: - pass + # try to parse the body as msgpack, if it's not json + if body_val is None: + try: + body_val = msgpack.loads(req.get_request_body()) + content_type = "application/msgpack" + except Exception: + pass if content_type is None: # try to parse the body as form data @@ -298,12 +299,13 @@ def main(override_args: Optional[Sequence[str]] = None): except json.decoder.JSONDecodeError: response_parsed = None - # try parsing the response as msgpack - try: - response_parsed = msgpack.loads(response_body) - response_content_type = "application/msgpack" - except Exception: - response_parsed = None + if response_parsed is None: + # try parsing the response as msgpack, if it's not json + try: + response_parsed = msgpack.loads(response_body) + response_content_type = "application/msgpack" + except Exception: + response_parsed = None if response_parsed is not None: resp_data_to_set = {