Decoding base64 while using GitHub API to Download a File

天大地大妈咪最大 提交于 2019-12-06 11:03:48

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'

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.

For some reason the Github APIs base64 encoded content doesn't decode properly at all the online base64 decoders I've tried from the front page of google.

Python works however:

import base64
base64.b64decode("ewogICAgInN3YWdnZXIiOiAiM...")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!