From 53c14f5c81351ed7ddab83a76cff97ddb01aff8d Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 4 Jun 2020 15:30:24 +0800 Subject: [PATCH] fix: try json loads request_body only if it is not None --- httprunner/client.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/httprunner/client.py b/httprunner/client.py index d3cace95..d396917e 100644 --- a/httprunner/client.py +++ b/httprunner/client.py @@ -43,20 +43,21 @@ def get_req_resp_record(resp_obj: Response) -> ReqRespData: # record actual request info request_headers = dict(resp_obj.request.headers) request_cookies = resp_obj.request._cookies.get_dict() - request_body = resp_obj.request.body - try: - request_body = json.loads(request_body) - except json.JSONDecodeError as ex: - # str: Unexpected UTF-8 BOM (decode using utf-8-sig) - capture_exception(ex) - except UnicodeDecodeError as ex: - # bytes/bytearray: request body in protobuf - capture_exception(ex) - except TypeError as ex: - # neither str nor bytes/bytearray, e.g. None - capture_exception(ex) - if request_body: + request_body = resp_obj.request.body + if request_body is not None: + try: + request_body = json.loads(request_body) + except json.JSONDecodeError as ex: + # str: Unexpected UTF-8 BOM (decode using utf-8-sig) + capture_exception(ex) + except UnicodeDecodeError as ex: + # bytes/bytearray: request body in protobuf + capture_exception(ex) + except TypeError as ex: + # neither str nor bytes/bytearray + capture_exception(ex) + request_content_type = lower_dict_keys(request_headers).get("content-type") if request_content_type and "multipart/form-data" in request_content_type: # upload file type