Converting Base64 to Base64Url with only Javascript?


Senior member
Jun 8, 2012

I am utilizing ServiceNow JavaScript to try to convert Base64 to Base64Url. I cannot use any dependencies since it's all within ServiceNow tools.

I came up with this and it seems to work with all the tests I used. Is there any glaring problems?

GlideStringUtil.base64Encode is a native way in ServiceNow to convert to base64. Once I have that, I make an attempt to convert it to base64url so I can pass it to Azure DevOps.

var bodyBase64 = "This is a string to convert to base64!!!!!!"
var bodyBase64 = GlideStringUtil.base64Encode(body);

function convertobase64url(bodyBase64) {
//replace any instance of / with _
bodyBase64 = bodyBase64.replace("/", "_");
bodyBase64 = bodyBase64.replace("/+", "-");
bodyBase64 = bodyBase64.replaceAll("=", "");
return bodyBase64;