I'm using this code for encode user_name and password and put it to headers.
def login_as_user(self,user):
encode_login = base64.b64encode(bytes(str(user['email'] + ":" +user['password']),'utf8'))
headers = { Config.API_AUTHORIZATION:"Basic "+str(encode_login)}
response = requests.requestd("POST", self.url_path , data="", headers=headers)
return response
With the
user_name = test@test.com and password = 1.
When I converted encode_login to str , the output always b'dGVzdEB0ZXN0LmNvbTox' , then when convert to string, it include b'. This makes my headers when requesting is wrong.
Please help, how can I solve this? Thanks.
You can simply decode your data to UTF8
by using decode()
.
For example, if your variable is:
a = b'dGVzdEB0ZXN0LmNvbTox'
# Or:
# a = a.decode('UTF8')
a = a.decode()
print(a)
Output:
>> 'dGVzdEB0ZXN0LmNvbTox'
Otherwise, this will print:
base64.b64encode(bytes("hello", 'utf8')).decode()
>>> 'aGVsbG8='
So, in your question, you have just to modify this line:
headers = { Config.API_AUTHORIZATION:"Basic " + str(encode_login)}
By:
headers = { Config.API_AUTHORIZATION:"Basic " + encode_login.decode()}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.