KB Article #180683
Sample base64 encode script inserts newlines into output
Problem
The base64 encode sample script shipped with the API Gateway inserts newlines into its output, which is undesirable when doing things like encoding HTTP Basic authentication credentials.
Resolution
This is the expected behavior of the function offered by com.vordel.common.base64. The sample scripts have been updated in 7.6.2 and now use a different class to do the encoding, but the behavior of the com.vordel.common.base64 class has not changed. Any scripts using the com.vordel.common.base64 class in later versions will have this issue.
To avoid the newlines, you can call java.util.Base64 as in this example:
//# sorceURL=Base64 Encode var script_name = "Base64 Encode"; // Nashorn syntax JavaScript to base64 encode data // // Input: Data to be base64 encoded ${raw.data} // Output: Base64 encoded data ${encoded.data} var imp = new JavaImporter(com.vordel.trace, com.vordel.circuit, java.util.Base64); with (imp) { function invoke(msg) { try { var data = msg.get("raw.data"); var bytes = new java.lang.String(data).getBytes(); var base64 = Base64.getEncoder().encodeToString(bytes); msg.put("encoded.data", base64); } catch (e) { Trace.error(script_name + " error: " + e.stack); throw new CircuitAbortException(e); // Used to follow the abort path on error. // Use return false; instead of the above if you want to use the false path on error. } return true; } };