From 6d96dde0308df205d0a65d058ada2db169b31985 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 22 Feb 2020 10:01:02 -0600 Subject: [PATCH 1/4] allow user to pass content_type to request method. --- adafruit_requests.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index 5bbd3d3..8b82cf1 100755 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -146,7 +146,7 @@ def iter_content(self, chunk_size=1, decode_unicode=False): # pylint: disable=too-many-branches, too-many-statements, unused-argument, too-many-arguments, too-many-locals -def request(method, url, data=None, json=None, headers=None, stream=False, timeout=1): +def request(method, url, data=None, json=None, headers=None, stream=False, timeout=1, content_type=None): """Perform an HTTP request to the given url which we will parse to determine whether to use SSL ('https://') or not. We can also send some provided 'data' or a json dictionary which we will stringify. 'headers' is optional HTTP headers @@ -212,6 +212,8 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo data = json_module.dumps(json) sock.send(b"Content-Type: application/json\r\n") if data: + if content_type is not None: + sock.send(b"Content-Type: %s\r\n" % content_type) sock.send(b"Content-Length: %d\r\n" % len(data)) sock.send(b"\r\n") if data: From 02feba30028c2fc6d2a60718edce7ce60f7ad3b2 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 22 Feb 2020 10:24:21 -0600 Subject: [PATCH 2/4] undo content_type parameter change --- adafruit_requests.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index 8b82cf1..5bbd3d3 100755 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -146,7 +146,7 @@ def iter_content(self, chunk_size=1, decode_unicode=False): # pylint: disable=too-many-branches, too-many-statements, unused-argument, too-many-arguments, too-many-locals -def request(method, url, data=None, json=None, headers=None, stream=False, timeout=1, content_type=None): +def request(method, url, data=None, json=None, headers=None, stream=False, timeout=1): """Perform an HTTP request to the given url which we will parse to determine whether to use SSL ('https://') or not. We can also send some provided 'data' or a json dictionary which we will stringify. 'headers' is optional HTTP headers @@ -212,8 +212,6 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo data = json_module.dumps(json) sock.send(b"Content-Type: application/json\r\n") if data: - if content_type is not None: - sock.send(b"Content-Type: %s\r\n" % content_type) sock.send(b"Content-Length: %d\r\n" % len(data)) sock.send(b"\r\n") if data: From 2a9e5fe85867813ae24eafd0553aafbdc5354fc9 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 22 Feb 2020 10:30:40 -0600 Subject: [PATCH 3/4] if data is dict send it as form data. --- adafruit_requests.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/adafruit_requests.py b/adafruit_requests.py index 5bbd3d3..faa65aa 100755 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -212,6 +212,12 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo data = json_module.dumps(json) sock.send(b"Content-Type: application/json\r\n") if data: + if type(data) is dict: + sock.send(b"Content-Type: application/x-www-form-urlencoded\r\n") + _post_data = "" + for k in data: + _post_data = "{}&{}={}".format(_post_data, k, data[k]) + data = _post_data[1:] sock.send(b"Content-Length: %d\r\n" % len(data)) sock.send(b"\r\n") if data: From 73852535283a01d3c51985fbcbabc0873ccbd95c Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Sat, 22 Feb 2020 10:59:52 -0600 Subject: [PATCH 4/4] using isinstance instead of type --- adafruit_requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index faa65aa..9a8667d 100755 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -212,7 +212,7 @@ def request(method, url, data=None, json=None, headers=None, stream=False, timeo data = json_module.dumps(json) sock.send(b"Content-Type: application/json\r\n") if data: - if type(data) is dict: + if isinstance(data, dict): sock.send(b"Content-Type: application/x-www-form-urlencoded\r\n") _post_data = "" for k in data: