Skip to main content

How to Base64url-Encode a String in Ruby with Base64.urlsafe_encode64

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Ruby's Base64 module encodes base64url with Base64.urlsafe_encode64: - and _ instead of + and /. Add padding: false and it also leaves out the = padding, which is the form JSON Web Tokens use.

Syntax​

Syntax
require 'base64'

<encoded> = Base64.urlsafe_encode64(<text>, padding: false)
<text> = Base64.urlsafe_decode64(<encoded>)
PartWhat it does
require 'base64'Loads the Base64 module, from the base64 gem that comes with Ruby.
urlsafe_encode64(<text>)Encodes the string with the URL-safe alphabet, with = padding.
padding: falseLeaves the padding out.
urlsafe_decode64(<encoded>)Decodes base64url text, with or without padding.

The base64 gem's documentation for urlsafe_encode64: "Returns the RFC-4648-compliant Base64-encoding of string. Per RFC 4648, the returned string will not contain the URL-unsafe characters + or /, but instead may contain the URL-safe characters - and _". And: "Optionally, you can suppress padding".

The Example​

The example encodes the text Hi?> with strict_encode64, with urlsafe_encode64, and with urlsafe_encode64 and padding: false, then decodes the result back. Those four characters were chosen because their base64 contains a / and padding.

Prerequisites​

  • Ruby. This guide uses Ruby 4.0.10, which comes with the base64 gem.

Step 1: Write the Code​

Create a file named main.rb:

main.rb, which requires base64 and puts strict_encode64, urlsafe_encode64 and urlsafe_encode64 with padding false of Hi?>, then urlsafe_decode64 of SGk_Pg.

main.rb
require 'base64'

text = 'Hi?>'

puts Base64.strict_encode64(text)
puts Base64.urlsafe_encode64(text)
puts Base64.urlsafe_encode64(text, padding: false)
puts Base64.urlsafe_decode64('SGk_Pg')

Step 2: Run It​

Terminal
ruby main.rb
Output
SGk/Pg==
SGk_Pg==
SGk_Pg
Hi?>

A zsh terminal where ruby main.rb prints SGk/Pg==, SGk_Pg==, SGk_Pg and Hi?>.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • strict_encode64 gave standard base64, SGk/Pg==.
  • urlsafe_encode64 swapped the / for _ but kept the padding.
  • With padding: false, the padding was gone too: SGk_Pg.
  • urlsafe_decode64 decoded the unpadded text back to Hi?>.

Common Mistakes​

MistakeWhat happensDo this instead
Forgetting require 'base64'uninitialized constant Base64 (NameError)Require it at the top of the file
Leaving out padding: false for a JWT pieceThe text ends in =Pass padding: false
Using encode64It adds a line break after every 60 characters of outputUse strict_encode64 or urlsafe_encode64

Troubleshooting​

SymptomLikely causeFix
uninitialized constant Base64The module wasn't loadedrequire 'base64'
The result ends in =padding: false is missingAdd it

FAQ​

Does urlsafe_decode64 need the padding?​

No. It decoded SGk_Pg without it. It also decoded plain base64, SGk/Pg==, without an error.

Do I need Bundler for require 'base64'?​

Not for a plain script: Ruby 4.0 comes with the base64 gem, and ruby main.rb loaded it. In a Bundler project, the gem has to be in the bundle, for example as a dependency of another gem.

Conclusion​

Base64.urlsafe_encode64(text, padding: false) gives the unpadded base64url that JWTs use, and Base64.urlsafe_decode64(encoded) turns it back, padding or not.

Additional Resources​