Convert byte string to base64-encoded string (output not being a byte string)
Try this:
def bytes_to_base64_string(value: bytes) -> str:
import base64
return base64.b64encode(value).decode('ASCII')
There is one misunderstanding often made, especially by people coming from the Java world. The bytes.decode('ASCII')
actually encodes bytes to string, not decodes them.
I couldn't find a decent answer which worked on converting bytes to urlsafe b64 encoded string, so posting my solution here.
Let's say you have an input:
mystring = b'\xab\x8c\xd3\x1fw\xbb\xaaz\xef\x0e\xcb|\xf0\xc3\xdfx=\x16\xeew7\xffU\ri/#\xcf0\x8a2\xa0'
Encode to base64
from base64 import b64encode # or urlsafe_b64decode
b64_mystring = b64encode(mystring)
this gives: b'q4zTH3e7qnrvDst88MPfeD0W7nc3/1UNaS8jzzCKMqA='
which still needs decoding, since bytes are not JSON serializable.
import requests
requests.get("https://google.com", json={"this": b64_mystring})
# raises "TypeError: Object of type bytes is not JSON serializable"
Hence we use:
from base64 import b64encode
b64_mystring = b64encode(mystring).decode("utf-8")
This gives us: q4zTH3e7qnrvDst88MPfeD0W7nc3/1UNaS8jzzCKMqA=
which is now JSON serializable (using json.dumps
).
What works for me is to change the b64encode
line to:
myObj = [base64.b64encode(data).decode('ascii')]
This is explained in https://stackoverflow.com/a/42776711 :
base64 has been intentionally classified as a binary transform.... It was a design decision in Python 3 to force the separation of bytes and text and prohibit implicit transformations.
The accepted answer doesn't work for me (Python 3.9) and gives the error:
Traceback (most recent call last):
File "/tmp/x.py", line 4, in <module>
myObj = [base64.b64encode(data)]
File "/usr/lib64/python3.9/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
Try
data = b'foo'.decode('UTF-8')
instead of
data = b'foo'
to convert it into a string.