From 0526d94f4a073cc4d15a02bdbbf447776d75f81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Slobodan=20Mi=C5=A1kovi=C4=87?= Date: Fri, 21 Oct 2016 22:05:42 -0700 Subject: [PATCH] Handle `bytes` in request parameters --- examples/har_dump.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/har_dump.py b/examples/har_dump.py index 560b9adca..3ba08968b 100644 --- a/examples/har_dump.py +++ b/examples/har_dump.py @@ -139,7 +139,7 @@ def response(flow): if flow.request.method in ["POST", "PUT", "PATCH"]: entry["request"]["postData"] = { "mimeType": flow.request.headers.get("Content-Type", "").split(";")[0], - "text": flow.request.content, + "text": _always_string(flow.request.content), "params": name_value(flow.request.urlencoded_form) } @@ -213,4 +213,12 @@ def name_value(obj): """ Convert (key, value) pairs to HAR format. """ - return [{"name": k, "value": v} for k, v in obj.items()] + return [{"name": _always_string(k), "value": _always_string(v)} for k, v in obj.items()] + +def _always_string(byte_or_str): + """ + Makes sure we get text back instead of `bytes` since json.dumps dies on `bytes` + """ + if isinstance(byte_or_str, bytes): + return byte_or_str.decode('utf8') + return byte_or_str