Skip to content

Draft: Strings need converted to unicode when creating multipart request #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sword2/http_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self, username, password):
self.password = password

def http_request(self, request):
request.add_header(self.auth_header, 'Basic %s' % base64.b64encode(self.username + ':' + self.password))
request.add_header(self.auth_header, 'Basic %s' % base64.b64encode((self.username + ':' + self.password).encode()).decode("utf-8"))
return request

https_request = http_request
Expand Down
17 changes: 10 additions & 7 deletions sword2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ def create_multipart_related(payloads):
"""
# Generate random boundary code
# TODO check that it does not occur in the payload data
bhash = md5(datetime.now().isoformat()).hexdigest() # eg 'd8bb3ea6f4e0a4b4682be0cfb4e0a24e'
bhash = md5(datetime.now().isoformat().encode()).hexdigest() # eg 'd8bb3ea6f4e0a4b4682be0cfb4e0a24e'
BOUNDARY = '===========%s_$' % bhash
CRLF = '\r\n' # As some servers might barf without this.
CRLF = b'\r\n' # As some servers might barf without this.
body = []
for payload in payloads: # predicatable ordering...
body.append('--' + BOUNDARY)
Expand All @@ -213,17 +213,20 @@ def create_multipart_related(payloads):
body.append('Content-Transfer-Encoding: base64')
body.append('')
if hasattr(payload['data'], 'read'):
body.append(b64encode(payload['data'].read()))
body.append(b64encode(payload['data'].read()).decode())
else:
body.append(b64encode(payload['data']))
body.append(b64encode(payload['data']).decode())
else:
body.append('')
if hasattr(payload['data'], 'read'):
body.append(b64encode(payload['data'].read()))
body.append(b64encode(payload['data'].read()).decode())
else:
body.append(b64encode(payload['data']))
try:
body.append(b64encode(payload['data']).decode())
except TypeError:
body.append(b64encode(payload['data'].encode("utf-8")).decode())
body.append('--' + BOUNDARY + '--')
body.append('')
body_bytes = CRLF.join(body)
body_bytes = CRLF.join([line.encode("utf-8") for line in body])
content_type = 'multipart/related; boundary="%s"' % BOUNDARY
return content_type, body_bytes