Decoding base64 while using GitHub API to Download a File

前端 未结 3 1297
执念已碎
执念已碎 2021-01-05 19:28

I am using the GitHub API to download a file from GitHub. I have been able to successfully authenticate as well as get a response from github, and see a base64 encoded stri

3条回答
  •  太阳男子
    2021-01-05 20:08

    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'
    

提交回复
热议问题