From 47735421b806f18ca07cba0a03c6e7077869fa97 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 16 Sep 2019 10:57:18 +0100 Subject: [PATCH] add `json_body` arg to Request to json-encode body, only return from `body` if not null --- src/utils/http.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/utils/http.py b/src/utils/http.py index 73780a58..ca27fc91 100644 --- a/src/utils/http.py +++ b/src/utils/http.py @@ -57,7 +57,7 @@ class Request(object): get_params: typing.Dict[str, str]={}, post_data: typing.Any=None, headers: typing.Dict[str, str]={}, - json: bool=False, allow_redirects: bool=True, + json: bool=False, json_body: bool=False, allow_redirects: bool=True, check_content_type: bool=True, parse: bool=False, detect_encoding: bool=True, @@ -72,6 +72,7 @@ class Request(object): self.headers = headers self.json = json + self.json_body = json_body self.allow_redirects = allow_redirects self.check_content_type = check_content_type self.parse = parse @@ -105,10 +106,13 @@ class Request(object): return headers def get_body(self) -> typing.Any: - if self.content_type == "application/json": - return _json.dumps(self.post_data) + if not self.post_data == None: + if self.content_type == "application/json" or self.json_body: + return _json.dumps(self.post_data) + else: + return self.post_data else: - return self.post_data + return None class Response(object): def __init__(self, code: int, data: typing.Any,