Base64Url encode and decode

I’m seeing more and more Base64Url encoded data being used, especially around JWT tokens. Linx supports Base64 functions in its Expressions but not Base64Url (yet). Luckily it is quite simple to do your own conversion with a Linx expression.

Base64 to Base64Url:
=Base64.Replace("/", "_").Replace("+", "-").TrimEnd('=')

Base64Url to Base64:
=Base64Url.Replace("_", "/").Replace("-", "+").PadRight(Base64Url.Length + (Base64Url.Length % 4 > 0 ? 4 - Base64Url.Length % 4 : 0), '=')

2 Likes

Also, If you need to decode a base64 encoded STR such as the Basic authentication credentials in HTTP requests, use the below expression:

= #base64EncodedString#.ToBytesFromBase64().ToString("UTF8")

The string YWRtaW46YWRtaW4= will result in admin:admin.

By using this method its giving me error… I have attached screen shot. Can you please guide.


t

Hi Reema,

Welcome to the Linx Community.

I would suggest splitting this step into 2 to make it easier to debug.

Thus I would add a List to the process and change it’s type to Byte and set its value to file_data.ToBytesFromBase64. Then I would add my String and set it’s value to List.ToString(“UTF8”)

Thank you for the quick response. Now its giving me error on list
Cannot assign type [List`1] to type [Byte]
I have read a json object from a service and its response contains one field with string64 data and now its not decoding.

Hi Reema,

My guess is that you’re assigning the wrong items to the wrong types. Drop us a mail at support@linx.software with the solution or screenshots of the solution and assignments.

Regards,
Dawie