feat: log request/response headers and body with indent

This commit is contained in:
debugtalk
2020-05-19 16:07:14 +08:00
parent d8ad62d7db
commit 7158128fef
2 changed files with 10 additions and 1 deletions

View File

@@ -7,6 +7,7 @@
- feat: make testsuite and run testsuite
- feat: testcase/testsuite config support getting variables by function
- feat: har2case with request cookies
- feat: log request/response headers and body with indent
**Fixed**

View File

@@ -1,3 +1,4 @@
import json
import time
import requests
@@ -32,12 +33,19 @@ def get_req_resp_record(resp_obj: Response) -> ReqRespData:
def log_print(req_or_resp, r_type):
msg = f"\n================== {r_type} details ==================\n"
for key, value in req_or_resp.dict().items():
msg += "{:<16} : {}\n".format(key, repr(value))
if isinstance(value, dict):
value = json.dumps(value, indent=4)
msg += "{:<8} : {}\n".format(key, value)
logger.debug(msg)
# record actual request info
request_headers = dict(resp_obj.request.headers)
request_body = resp_obj.request.body
try:
request_body = json.loads(request_body)
except json.JSONDecodeError:
pass
if request_body:
request_content_type = lower_dict_keys(request_headers).get("content-type")