Encode / Decode base64 with javascript?
Hi,
I need to decode base64 encoded output of a rest service and compare the decoded result with a value from a database, but I don't know how.
I understood that it is possible to decode with javascript, but how can this be done? This is the scenario:
- JSON call to a REST service returns a encoded value.
- I saved this value in the JSON databank.
- Now I need to base64 decode this value --> how? How can I use the stored value in Javascript?
- After decoding this value, I need to check if this is similar to the value in the database. --> How can I compare the result of a javascript with another value?
I have no idea how to use the stored value in javascript for decoding, and how to compare this output. Any advice is appreciated.
0
Comments
Hello Señor Test,
Have you had a chance to look at the Extensibility (Scripting) section of your SOAtest Userguide? You can find this in your Installation Directory or if you navigate to the top toolbar in Parasoft>Help>Help Contents, you will find a plethora of information available to you!!
Now, to encode/decode a value, you should be able to use the following method I found online:
Please look over the Extensibility section of your SOAtest Userguide and try to implement the above! Let us know how it goes!
Hi Omar,
I am now able to use a stored value from a data bank in Javascript, so thanks a lot for that!
However, I'm stuck at the decoding part. I was hoping to use the 'atob' function within javascript in the extension tool, but it seems that it's not supported by parasoft. I'm getting the following error:
Runtime error The function 'atob' is not defined for object '[object Object]'
detected at line 7 of function 'getKeywords' in string starting with: 'var decoded =
Packages.soaptest.api.SOAPUtil '...
Do you know how we can make the atob function work in the extension tool?
About the javax.xml.bind suggestion, I do not know how to make it work
I added the jar to the classpath in the preferences window and from there on I'm lost. I have no experience with java and have no clue what to do next
So if the atob function cannot work in Parasoft, can you guide me through making the java.xml.bind work?
Señorita Test
Forgive me Señorita,
I have not used the 'atob' function myself, but the following Groovy script should do the trick. No additional jars required.
Thanks Omar,
Unfortunately I am getting a nullpointerexception. Is there an easy way to fix this?
Error Message:
DataSource: Base64 (row 1): Error during script execution. View Details for more
information.
java.lang.NullPointerException.
Hi Test,
This error normally indicates that you are attempting to "grab' an element that does not exist.
In the line:
c1 = context.getValue("Generated Data Source", "item_var");
You are attempting to extract a value from a variable named "item_var", which would have been created using a data bank.
If you see this error, please make certain that you are calling the exact same name as the Variable you created with your data bank, otherwise when attempting to pull the value from the context it will look at a null point.
The name here:

should match the name here:
c1 = context.getValue("Generated Data Source", "item_var");
Best regards,
Ramiro
As of Java 8, I'd recommend using java.util.Base64.
DatatypeConverter from JAXB is only needed for Java 7.
Hey everyone,
The script that Omar provided can also be used for encoding, with a few small modification:
The above script will take the take the value that is associated with the test suite variable 'x' and encode the value. It then returns the value to the next tool in the chain and sets a new value for test suite variable 'x'(this only last for this test's execution). The new 'x' value can then be used for the rest of the test in this execution.
I agree with Benken that "java.util.Base64.DatatypeConverter" is preferred.
If you are still using SOATest 9.9 and have not changed the Java version then the above will work.
The name is "Benken"
You didn't see anything..... It never happened