diff --git a/httpie_hmac/httpie_hmac.py b/httpie_hmac/httpie_hmac.py index a50977e..0057fc0 100644 --- a/httpie_hmac/httpie_hmac.py +++ b/httpie_hmac/httpie_hmac.py @@ -10,6 +10,7 @@ import hashlib import hmac import importlib.machinery +import io import os import requests import types @@ -143,6 +144,8 @@ def __call__(self, r): content_type = r.headers.get('content-type') if not content_type: content_type = '' + else: + content_type = content_type.decode('utf-8') # If content-md5 is already given, use it, otherwise calculate # it ourselves and add it to the headers @@ -150,11 +153,21 @@ def __call__(self, r): if not content_md5: if content_type and r.body: m = hashlib.md5() + body = r.body + # If we have a buffer, convert it in to a string + if type(r.body) == io.BufferedReader: + body = body.read() + r.body = body m.update(r.body) - content_md5 = base64.b64encode(m.digest()).rstrip() + content_md5 = base64 \ + .b64encode(m.digest()) \ + .rstrip() \ + .decode('utf-8') r.headers['Content-MD5'] = content_md5 else: content_md5 = '' + else: + content_md5 = content_md5.decode('utf-8') # If date is given already, use it - otherwise generate it # ourselves and add it to the headers @@ -163,6 +176,8 @@ def __call__(self, r): now = datetime.datetime.utcnow() http_date = now.strftime('%a, %d %b %Y %H:%M:%S GMT') r.headers['Date'] = http_date + else: + http_date = http_date.decode('utf-8') # Get the path from the UL url = urlparse(r.url) diff --git a/pyproject.toml b/pyproject.toml index ba384b5..092a15f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "httpie-hmac" -version = "1.1.1" +version = "1.2.0" authors = [ {name = "Martyn Pittuck-Schols", email = "martyn@rustfoo.com"}, ]