Ask a Question

Base64 encoded string

SOLVED
Philip_Baird
Community Expert

Base64 encoded string


Hi, as part of a XHR web service call I am testing, I am required to send basic Authorization in the header encoded as Base64.


 


I couldn't find a way to do this from a Test Complete object so came up with the following using the dotNET object:


 


function getEncodedCredentials( userName, password ) {


  var textBytes = dotNET.System_Text.Encoding.UTF8.GetBytes_2( userName + ":" + password );


  var encodedText = dotNET.System.Convert.ToBase64String( textBytes ); 


  


  return encodedText;


}


 


which is used such as:


 


Log.Message( getEncodedCredentials( "fred", "fl1ntst0ne" ) );


 


My question is this, is their another way to do this in Test Complete that does not require a call to the CLR Bridge?


 


Regards,


Phil Baird

9 REPLIES 9
AlexKaras
Champion Level 3

Hi Phil,



I used .Net-based approach in several projects and it worked fine.

However, the following request http://www.google.com/search?q=base64+encode+string+jscript+vbscript returned quite a few results the first of which http://stackoverflow.com/questions/496751/base64-encode-string-in-vbscript looks quite interesting (though I did not verified what people have talked there about).
Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
Philip_Baird
Community Expert


Thanks Alexei, after a quick look I think I will stick with the .Net approach.


 


Phil

AlexKaras
Champion Level 3

The reason for my decision was exactly the same 😉
Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
Afshal
New Contributor

Hi, Can i get the same equivalent script for decoding a string. 

AlexKaras
Champion Level 3

Hi,

 

If your test project is JScript/Java Script/C#Script-based, then the code from https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript can be used as it is.

For the project that uses some other language you can put the referenced code into Script Extension and use it from there.

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================

Thanks, But i need the script in this format for decoding too


var string='test';
function main()
{
  getEncodedValue(string);
}
function getEncodedValue(string)
{
  var textBytes = dotNET.System_Text.Encoding.UTF8.GetBytes_2(string);
  var encodedText = dotNET.System.Convert.ToBase64String(textBytes);
//  return encodedText;
  Log.Message(encodedText);
}
 

 
AlexKaras
Champion Level 3

Hi,

 

The code I have referenced does not use .Net but is pure Java-based.

If you declare it as a public variable (as in the code sample), then use Base64.encode() to encode the string and Base64.decode() to decode encoded one.

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================

Hey Alex,

 

I need a Function containing only Decoding  part in java Script. I refered your Link Containing both encoding and decoding functions together and I'm not able to divide them seperately So can  you help me with Seperate functions for both encoding and decode functionalities.

 

Regards,

Rohit Sen

Hi Rohit,

 

The referred link contains minified code (with extra spaces and indentations removed). You may expand it with the help of some Java Script formatter (or manually) and remove the part that you don't need.

 

P.S. Out of curiosity: is it really a problem if encoding function is present in the code?

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
cancel
Showing results for 
Search instead for 
Did you mean: