1

I am getting the following error when I run my Python REST API script:

Traceback (most recent call last):
  File "api_tufin_4.py", line 4, in <module>
    b64Val = base64.b64encode(usrPass)
  File "C:\Program Files\Python35\lib\base64.py", line 62, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]
TypeError: a bytes-like object is required, not 'str'

I am using the following code:

import requests, base64

usrPass = "user:pass"
b64Val = base64.b64encode(usrPass)
api_URL = 'api-url'
r=requests.post(api_URL, 
                headers={"Authorization": "Basic %s" % b64Val},
                data=payload)
1
  • Please take a look at the docs. Commented Feb 17, 2019 at 16:18

1 Answer 1

1

base64.b64encode works on binary data (bytes), not text data (str).

Make your username/password a bytes literal and you're fine. If it's ASCII, the translation is trivial:

usrPass = "user:pass"

becomes:

usrPass = b"user:pass"

The leading b makes it a bytes literal.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.