I want to read in a file, get it's size in bytes, and get its contents as a base64 string.
I was thinking to use the enclosed javascript functionality of karate, because it's so convenient.
I tried this:
* def fileToAdd = 'test.pdf'
* def fileToAddAsString = read(fileToAdd)
* def fileToAddByteSize = (fileToAddAsString.length)
* def fileToAddAsBase64 = (btoa(fileToAddAsString))
but the last line failed, as shown by the following output:
>>> failed features:
js failed:
>>>>
01: (btoa(fileToAddAsString))
<<<<
org.graalvm.polyglot.PolyglotException: ReferenceError: "btoa" is not defined
- <js>.:program(Unnamed:1)
Any ideas how to work-around this? I assume that the builtin javascript engine doesn't have the library that defines btoa(), but i don't know if that can be overcome, and if so, how.
Since my file is static, I could hardcode the base64 encoding of its contents, but I'd rather make it easier to change the file in the future, by having the code compute it for me, so i don't have to update the fileToAddAsBase64.
Please advise. Thanks!