0
username = 'root'
password = '1234'
auth_str = '%s:%s' % (username, password)
b64_auth_str = base64.b64encode(auth_str.encode())

headers = {'Authorization': 'Basic %s' % b64_auth_str}

Above is my code for base encoding. My header become {'Authorization': "Basic b'cm9vdDoxMjM0'"}. But, what I need is {'Authorization': 'Basic cm9vdDoxMjM0'}

Why extra char and how to remove those?

1 Answers1

0

It should be as simple as decoding the result of base64.b64encode

import base64

creds = b'root:1234'
b64_auth_str = base64.b64encode(creds).decode('ascii')
headers = {'Authorization': f'Basic {b64_auth_str}'}

print(headers)  # {'Authorization': 'Basic cm9vdDoxMjM0'}
kingkupps
  • 3,284
  • 2
  • 16
  • 28