GitHub API返回错误的文件内容

Nik*_*kov 5 rest github github-api

我正在尝试使用GitHub API创建文件.文件应包含单个字节0xDD.我正在使用以下命令:

http PUT https://api.github.com/repos/koluch/test/contents/data.txt "message"="New file" "content"="3Q==" Authorization:"token <OAUTH_TOKEN>"
Run Code Online (Sandbox Code Playgroud)

3Q==0xDD字节的Base64表示.该请求返回200; 但是,当我尝试使用GET请求检索文件时...

http GET https://api.github.com/repos/koluch/test/contents/data.txt 
Run Code Online (Sandbox Code Playgroud)

...它返回以下JSON:

{
    "_links": {
        "git": "https://api.github.com/repos/koluch/test/git/blobs/4e3bc519eef7d5a35fff67687eaee65616832e45",
        "html": "https://github.com/koluch/test/blob/master/data.txt",
        "self": "https://api.github.com/repos/koluch/test/contents/data.txt?ref=master"
    },
    "content": "77+9\n",
    "download_url": "https://raw.githubusercontent.com/koluch/test/master/data.txt",
    "encoding": "base64",
    "git_url": "https://api.github.com/repos/koluch/test/git/blobs/4e3bc519eef7d5a35fff67687eaee65616832e45",
    "html_url": "https://github.com/koluch/test/blob/master/data.txt",
    "name": "data.txt",
    "path": "data.txt",
    "sha": "4e3bc519eef7d5a35fff67687eaee65616832e45",
    "size": 1,
    "type": "file",
    "url": "https://api.github.com/repos/koluch/test/contents/data.txt?ref=master"
}
Run Code Online (Sandbox Code Playgroud)

字段content包含77+9\n值,这不是我的0xDD字节.当我使用下载URL时,一切都很好.

有人知道发生了什么吗?

小智 2

我仍然在 github 支持的循环中。到目前为止还没有任何进展。但我想我知道问题所在。看起来 github 试图猜测文件的文件类型,如果它认为它是文本文件,则使用检测到的编码来创建缓冲区,然后再将其编码为 base64。

这至少是我从原始请求的响应标头中看到的。我的解决方法是检查文件大小,如果不匹配,则使用原始 api 再次获取 blob:

      const res = await octokit.repos.getContents(params);
      let data = Buffer.from(res.data.content, 'base64');
      if (data.length !== res.data.size) {
        log.debug(`base64 decoded content length doesn't match: ${data.length} != ${res.data.size}`);
        log.debug(`fetching content from ${res.data.download_url}`);
        const result = await rp.get({
          url: res.data.download_url,
          resolveWithFullResponse: true,
          encoding: null,
        });
        data = result.body;
      }
Run Code Online (Sandbox Code Playgroud)