Skip to main content

How to Base64url-Encode in Go with base64.RawURLEncoding

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Go's encoding/base64 package has four ready-made encodings. base64.RawURLEncoding is the one JSON Web Tokens use: the URL-safe alphabet, with - and _ instead of + and /, and no = padding.

Syntax​

Syntax
<text> := base64.RawURLEncoding.EncodeToString(<bytes>)
<bytes>, <err> := base64.RawURLEncoding.DecodeString(<text>)
PartWhat it does
base64.StdEncodingStandard base64, with +, / and padding.
base64.URLEncodingURL-safe alphabet, with padding.
base64.RawURLEncodingURL-safe alphabet, without padding.
EncodeToString(<bytes>)Encodes a []byte and returns the text.
DecodeString(<text>)Decodes the text and returns the bytes and an error.

The Go documentation for encoding/base64: "RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648. It is typically used in URLs and file names."

The Example​

The example encodes the text Hi?> with three of the encodings, then decodes the base64url form back into text. Those four characters were chosen because their base64 contains a / and padding.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which encodes the bytes of Hi?> with StdEncoding, URLEncoding and RawURLEncoding, then decodes SGk_Pg with RawURLEncoding and prints the text or the error.

main.go
package main

import (
"encoding/base64"
"fmt"
)

func main() {
data := []byte("Hi?>")

fmt.Println(base64.StdEncoding.EncodeToString(data))
fmt.Println(base64.URLEncoding.EncodeToString(data))
fmt.Println(base64.RawURLEncoding.EncodeToString(data))

decoded, err := base64.RawURLEncoding.DecodeString("SGk_Pg")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(decoded))
}

Step 2: Run It​

Terminal
go run main.go
Output
SGk/Pg==
SGk_Pg==
SGk_Pg
Hi?>

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

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

What Happened​

  • StdEncoding gave SGk/Pg==: a / and two = of padding.
  • URLEncoding swapped the / for _ but kept the padding.
  • RawURLEncoding also left the padding out: SGk_Pg.
  • RawURLEncoding.DecodeString turned SGk_Pg back into Hi?>.

Common Mistakes​

MistakeWhat happensDo this instead
Decoding base64url text with StdEncodingillegal base64 data at input byte 3 for SGk_Pg, at the _Decode with the encoding that wrote it
Decoding padded text with RawURLEncodingillegal base64 data at input byte 6 for SGk_Pg==, at the first =Use URLEncoding for padded text
Ignoring the error from DecodeStringA bad input goes on as empty or partial bytesCheck err before using the bytes

Troubleshooting​

SymptomLikely causeFix
illegal base64 data at input byte NThe text uses another alphabet or padding than the encoding expectsMatch the encoding to the text
Numbers printed instead of textThe decoded []byte was printed directlyPrint string(decoded)

FAQ​

Which encoding do JWTs use?​

RawURLEncoding: RFC 7515 defines JWS's base64url "with all trailing '=' characters omitted".

Can I decode padded base64url?​

Yes, with base64.URLEncoding: it decoded SGk_Pg== to Hi?>.

Conclusion​

base64.RawURLEncoding.EncodeToString(data) gives unpadded base64url, the form JWTs use, and DecodeString turns it back. Pick the encoding that matches the text, because each one rejects the others' characters or padding.

Additional Resources​