How to do Basic Auth with base64?

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?

Maybe you need to pass the values as a string?

${AuthorizationHeader}= Evaluate base64.b64encode("${usernameEXT}:${passwordEXT}")
1 Like

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?

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

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

Maybe the simplest solution would be:

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

or

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