I experimented a bit and found a solution by using a different base64 decoding library as follows:

var base64 = require('js-base64').Base64;
var contents = base64.decode(res.content);

I am not sure if it is mandatory to have an encoded string length divisible by 4 (clearly my 15263 character length string is not divisible by 4) but the alternate library decoded the string properly.

A second solution which I also found to work is specific to how to use the GitHub API. By adding the following to the GitHub API call header, I was also able to get the decoded file contents:

'accept': 'application/vnd.github.VERSION.raw'
Answer from Eric Broda on Stack Overflow
🌐
Online Tools
emn178.github.io › online-tools › base64_decode.html
Base64 Decode - Online Tools
Decode standard Base64, Base64URL, unpadded, IMAP-style, or custom-alphabet data to text or binary bytes online.
🌐
GitHub
gist.github.com › 3015050
Encode/Decode base64 URL params · GitHub
Encode/Decode base64 URL params. GitHub Gist: instantly share code, notes, and snippets.
Author: brianloveswords
🌐
GitHub
github.com › topics › base64-decoding
base64-decoding · GitHub Topics · GitHub
arm compression jpeg virtual-machine jit transform base64-encoding base64-decoding bmp-image data-decoding compression-models ... The smallest and fastest Base64 implementation in JavaScript based on `atob` and `btoa` from browser native or `Buffer` from node · base64 base64url base64-encoding base64-decoding js-base64
Starred by 116 users
Forked by 18 users
Languages: JavaScript
🌐
GitHub
github.com › gfoidl › Base64
GitHub - gfoidl/Base64: Base64 encoding / decoding with SIMD-support, also base64Url · GitHub
A .NET library for base64 encoding / decoding, as well as base64Url support. Encoding can be done to buffers of type byte (for UTF-8) or char.
Starred by 60 users
Forked by 7 users
Languages: C# 96.7% | Shell 3.3%
Top answer
1 of 3
18

I experimented a bit and found a solution by using a different base64 decoding library as follows:

var base64 = require('js-base64').Base64;
var contents = base64.decode(res.content);

I am not sure if it is mandatory to have an encoded string length divisible by 4 (clearly my 15263 character length string is not divisible by 4) but the alternate library decoded the string properly.

A second solution which I also found to work is specific to how to use the GitHub API. By adding the following to the GitHub API call header, I was also able to get the decoded file contents:

'accept': 'application/vnd.github.VERSION.raw'
2 of 3
13

After much experimenting, I think I nailed down the difference between the working and broken base64 decoding.

It appears GitHub Base-64 encodes with:

  • UTF-8 charset
  • Base 64 MIME encoder (RFC2045)

As opposed to a "basic" (RFC4648) Base64 encoder. Several languages seem to default to the basic encoder (including Java, which I was using). When I switched to a MIME encoder, I got the full contents of the file un-garbled. This would explain why switching libraries in some cases fixed the issue.

I will note the contents field contained newline characters - decoders are supposed to ignore them, but not all do, so if you still get errors, you may need to try removing them.

The media-type header will do the job better, however in my case I am trying to use the API via a GitHub App - at time of writing, GitHub requires a specific media type be used when doing that, and it returns the JSON response.

🌐
GitHub
github.com › donavon › base64it
GitHub - donavon/base64it: A simple base64 encoder/decoder · GitHub
A simple base64 encoder/decoder that works with modern browsers, legacy browsers, and Node. It supports strict mode, URL safe encoding/decoding, and optional line lengths. ... Standard 'base64' (i.e. strict) (see RFC 4648 § 4) Standard 'base64url' ...
Author: donavon
Author: mathiasbynens
Find elsewhere
Starred by 12 users
Forked by 3 users
Languages: C 68.8% | R 31.2%
🌐
GitHub
github.com › topics › base64-decoder
base64-decoder · GitHub Topics · GitHub
Base64 Encoder & Decoder — Encode or decode Base64 with live auto-detection, full UTF-8/Unicode support, URL-safe mode, drag-and-drop files, JSON/XML pretty-print, and batch ZIP export — all processed privately in your browser.
Author: simonwhitaker
🌐
GitHub
gist.github.com › alvis › 89007e96f7958f2686036d4276d28e47
bash: base64url encode and decode · GitHub
bash: base64url encode and decode. GitHub Gist: instantly share code, notes, and snippets.
🌐
GitHub
github.com › Spomky-Labs › base64url
GitHub - Spomky-Labs/base64url: Base64 URL Safe Encoding/Decoding PHP library · GitHub
This library aims to provide a fast Base64 URL Safe encoder/decoder as described by the RFC 4648.
Author: Spomky-Labs
🌐
GitHub
github.com › jpfleury › base64url
GitHub - jpfleury/base64url: Bash encoder/decoder for the base64url format · GitHub
base64url is a Bash encoder/decoder for the base64url format (base64 with an URL and filename safe alphabet).
Author: jpfleury
Author: digitalbazaar
🌐
Base64Encode.org
base64encode.org › enc › github
Base64 Encoding of "github" - Online
Bookmark this page for instant access next time. Meet Base64 Decode and Encode, a simple online tool that does exactly what it says: decodes from Base64 encoding as well as encodes into it quickly and easily.