# How to do Basic Auth with base64?

**URL:** <https://forum.robotframework.org/t/how-to-do-basic-auth-with-base64/6997>\
**Category:** RequestsLibrary\
**Created:** [13 March 2024 17:44 UTC](https://forum.robotframework.org/t/how-to-do-basic-auth-with-base64/6997 "2024-03-13T17:44:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vitaly](https://avatars.discourse-cdn.com/v4/letter/v/838e76/32.png) [@Vitaly](https://forum.robotframework.org/u/Vitaly)\
**Post date:** [13 March 2024 17:44 UTC](https://forum.robotframework.org/t/how-to-do-basic-auth-with-base64/6997/1 "2024-03-13T17:44:04Z")

</div>

Hi!  
I am trying to create a post request for Basic Auth.  
I tried next:

```
    ${UserPassword} Create Dictionary username=${usernameEXT} password=${passwordEXT}
    Create Session MySession http://111.11.11.11:8080 auth=${UserPassword}
    ${headers} Create Dictionary Content-Type=application/zip 
    ${response2} POST On Session MySession url=http://111.11.11.11:8080/api/external/send-document data=${file} headers=${headers}

```

But I got 403 error. After checking postman I found out that POST has header Authorization with value Basic UUUUUUUUUUUUUUUUUUUUU==. As I understand it is a base 64 from user:password.

My question: how do I make base64 in robot?  
I tried:

`${AuthorizationHeader}= Evaluate base64.b64encode(${usernameEXT}:${passwordEXT})`

But got error: Evaluating expression ‘base64.b64encode(user:password)’ failed: SyntaxError: invalid syntax (, line 1)

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![HelioGuilherme66](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/helioguilherme66/32/34_2.png) [@HelioGuilherme66](https://forum.robotframework.org/u/HelioGuilherme66)\
**Post date:** [13 March 2024 20:46 UTC](https://forum.robotframework.org/t/how-to-do-basic-auth-with-base64/6997/2 "2024-03-13T20:46:07Z")

</div>

> [@Vitaly](#):
>
> What am I doing wrong?

Maybe you need to pass the values as a string?

```python
${AuthorizationHeader}= Evaluate base64.b64encode("${usernameEXT}:${passwordEXT}")

```

---

<div class="post-metadata">

**Author:** ![Vitaly](https://avatars.discourse-cdn.com/v4/letter/v/838e76/32.png) [@Vitaly](https://forum.robotframework.org/u/Vitaly)\
**Post date:** [14 March 2024 08:56 UTC](https://forum.robotframework.org/t/how-to-do-basic-auth-with-base64/6997/3 "2024-03-14T08:56:51Z")

</div>

Yes, we became closer to answer. Now I got error:  
Evaluating expression ‘base64.b64encode(“User:Password”)’ failed: TypeError: a bytes-like object is required, not ‘str’

I tried to create bytes-like object by:

`${byte}= Evaluate bytes("${usernameEXT}:${passwordEXT}", 'utf-8') ${AuthorizationHeader}= Evaluate base64.b64encode("${byte}")`

But still get the error: Evaluating expression ‘base64.b64encode(“User:Password”)’ failed: TypeError: a bytes-like object is required, not ‘str’

I tried “${byte}” and ${byte} (in this case I got error “invalid syntax”)  
What should I use now?

---

<div class="post-metadata">

**Author:** ![HelioGuilherme66](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/helioguilherme66/32/34_2.png) [@HelioGuilherme66](https://forum.robotframework.org/u/HelioGuilherme66)\
**Post date:** [14 March 2024 10:14 UTC](https://forum.robotframework.org/t/how-to-do-basic-auth-with-base64/6997/4 "2024-03-14T10:14:57Z")

</div>

> [@Vitaly](#):
>
> ${byte}= Evaluate bytes(“${usernameEXT}:${passwordEXT}”, ‘utf-8’) ${AuthorizationHeader}= Evaluate base64.b64encode(“${byte}”)

You are doing a similar error here. You pass a string, and in this case you want to not use the quotes 😃

You should try doing this kind of troubleshooting in a Python interactive prompt, or using a script.

Maybe the simplest solution would be:

```python
${AuthorizationHeader}= Evaluate base64.b64encode(b"${usernameEXT}:${passwordEXT}")

```

or

```python
${byte}= Evaluate bytes("${usernameEXT}:${passwordEXT}", 'utf-8')
${AuthorizationHeader}= Evaluate base64.b64encode(${byte})

```
