Strange Newline in base64 Encoded String in Ruby
Was working on a project today and ran into something weird. When I was base64 encoding a string in Ruby, I kept getting these mysterious \n characters in my encoded output.
Here's what was happening:
require 'base64'
str = "Hello World! This is a longer string to demonstrate the issue."
encoded = Base64.encode64(str)
puts encoded
# Output: SGVsbG8gV29ybGQhIFRoaXMgaXMgYSBsb25nZXIgc3RyaW5nIHRvIGRlbW9u\nc3RyYXRlIHRoZSBpc3N1ZS4=\n
Notice that \n at the end? And sometimes in the middle too for longer strings?
Turns out Base64.encode64 automatically adds newlines every 60 characters to make the output more readable. But when you're trying to use this in web applications or APIs, those newlines can mess things up.
The fix is simple:
# Use strict_encode64 instead
encoded = Base64.strict_encode64(str)
puts encoded
# Output: SGVsbG8gV29ybGQhIFRoaXMgaXMgYSBsb25nZXIgc3RyaW5nIHRvIGRlbW9uc3RyYXRlIHRoZSBpc3N1ZS4=
Or if you want to stick with encode64, just strip the newlines:
encoded = Base64.encode64(str).gsub(/\n/, '')
Alternative approaches:
# Method 1: Using chomp to remove trailing newline only
encoded = Base64.encode64(str).chomp
# Method 2: Using tr to remove all newlines (faster than gsub)
encoded = Base64.encode64(str).tr("\n", "")
# Method 3: Using delete (even faster for single character)
encoded = Base64.encode64(str).delete("\n")
The strict_encode64 method is definitely the cleanest approach though, since it doesn't add newlines in the first place.
This cost me a couple hours of debugging. Ruby's documentation could be clearer about this behavior!
Posted this question on Stack Overflow and it seems I'm not the only one who got confused by this.